Innovative way for checking if number has only one on bit in signed int

I'd recommend you take a look at the Bit Twiddling Hacks page and choose the most suitable option under "Determining if an integer is a power of 2" or "Counting bits set".


return x == (x & -x);

This answer works because of the way two's complement notation is designed.

First, an example. Assume we have 8-bit signed integers.

00010000  =  16
11110000  = -16

The bitwise and will give you 00010000 as a result, equal to your original value! The reason that this works is because when negating in 2's complement, first invert all the bits, then add 1. You'll have a bunch of zeros and a bunch of carries until a one falls into place. The bitwise and then checks if we have the right bit set.

In the case of a number that isn't a power of two:

  00101010  =  42
& 11010110  = -42
----------
  00000010 !=  42

Your result will still have only a single bit, but it won't match the original value. Therefore your original value had multiple bits set.

Note: This technique returns true for 0, which may or may not be desirable.


This is a famous problem

(x & x-1) == 0

Power of 2 from Wiki : here

64 = 01000000 (x)
63 = 00111111 (x-1)
______________
&  = 00000000  == 0
______________ 

Case when some other bits are ON

18 = 00010010 (x)
17 = 00010001 (x-1)
______________
&  = 00010000  != 0
______________ 

Tags:

Algorithm

Byte