Here's my code:

is this true? I tried it and it doesn't work as it's supposed to be. So is there another easier way?Code:`((num & (num - 1)) == 0)`

Printable View

- 03-26-2009-EquinoX-checking if a number if a power of 2
Here's my code:

Code:`((num & (num - 1)) == 0)`

- 03-26-2009whiteflags
Check if the square root is a whole number.

- 03-26-2009matsp
It should work. What numbers are you using?

--

Mats - 03-26-2009brewbuck
- 03-26-2009-EquinoX-
I am trying to use 3

- 03-26-2009matsp
- 03-26-2009-EquinoX-
oh nvm.. I found my self a stupid little mistake :D

- 03-26-2009whiteflags
- 03-26-2009sean
Power of two means two raised to a power, not a number raised to the second power. It's 2^x not x^2.

edit: The reason that first equation works is that any power of a number will be 1000 in that number system. 10, 100, 1000 are all powers of ten in the base-ten system. In base-two, they are all powers of two. Subtract one, and there will be a one in place of all the zeroes. 100 - 1 = 011. 100 & 011 = 0. - 03-27-2009iMalc
- 03-27-2009vart