Today I needed a simple algorithm for checking if a number is a power of 2.
The algorithm needs to be:
ulong
int isPowerOfTwo(unsigned int x) { return ((x != 0) && ((x & (~x + 1)) == x)); }
This is really fast. It takes about 6 minutes and 43 seconds to check all 2^32 integers.