title: Query about working out whether number is a power of 2
body: <p>Using the classic code snippet:</p>
  
  <p>if (x &amp; (x-1)) == 0</p>
  
  <p>If the answer is 1, then it is false and not a power of 2. However, working on 5 (not a power of 2) and 4 results in:</p>
  
  <p>0001 1111
  0001 1111
  0000 1111</p>
  
  <p>That's 4 1s.</p>
  
  <p>Working on 8 and 7:</p>
  
  <p>1111 1111
  0111 1111</p>
  
  <p>0111 1111</p>
  
  <p>The 0 is first, but we have 4.</p>
  
  <p>In this link (<a href="http://www.exploringbinary.com/ten-ways-to-check-if-an-integer-is-a-power-of-two-in-c/" rel="nofollow noreferrer">http://www.exploringbinary.com/ten-ways-to-check-if-an-integer-is-a-power-of-two-in-c/</a>) for both cases, the answer starts with 0 and there is a variable number of 0s/1s. How does this answer whether the number is a power of 2?</p>