c - How does this bitwise operation check for a power of 2? -
I'm looking at some code that should be trivial - but my math has failed me badly here.
Here is a situation that examines a number if someone uses the following 2 using the following:
if ((num! = 1) & Amp; (num & amp; num - 1) My question is, how does a bit wise use and determines between num and num - 1 if 2 is the power of number 2?
2 Any power of zero 1 is all: ( 2 N - 1 = 111 .... b )
2 = 2 ^ 1 2-1 = 1 (1b) 4 = 2 ^ 2 4-1 = 3 (11b) 8 = 2 ^ 3 8-1 = 7 (111b) For example, take 8, 1000 & amp; 0111 = 0000 so if expression test is not the power of any number 2.
Comments
Post a Comment