int isPowerOfTwo (unsigned int x) { return ((x != 0) && !(x & (x - 1))); }
It should be: ((x>0) && ((x & -x) == x))
Clearly we want to know if x is an integer power of 2.
Edit: Yes, 1 << bitsize is undefined. But unsigned integers actually do have well-defined semantics on overflow, and multiplying by 2 enough times really does produce zero.
If you meant 1 << (bitsize), that's undefined behaviour in C