c++bitwise-and

Meaning of bitwise and(&) of a positive and negative number?


Can anyone help what n&-n means?? And what is the significance of it.


Solution

  • I believe it is a trick to figure out if n is a power of 2. (n == (n & -n)) IFF n is a power of 2 (1,2,4,8).