Power of Two
(This is a question for an Internship of NVIDIA, Feb. 07, 2007)
#include <stdio.h>
bool IsPowerOfTwo(unsigned int value);
void main()
{
unsigned int inNumber = 6;
bool bResult = false;
bResult = IsPowerOfTwo(inNumber);
if (bResult)
printf("This number is Power of two!\n");
else
printf("This number is NOT Power of two!\n");
}
// http://en.wikipedia.org/wiki/Power_of_two
bool IsPowerOfTwo(unsigned int value)
{
// Moreover, we should check the case of that 'value' is 0(zero)
return ((value != 0) && ((value & (value - 1)) == 0));
}