This post will discuss a few related problems related to unsetting the rightmost set bit of a number.

How to unset the rightmost set bit of a number?

Practice this problem

The expression n & (n-1) will turn off the rightmost set bit of a number n. The expression n-1 will have all the bits flipped after the rightmost set bit of n (including the rightmost set bit). So, n & (n-1) will result in the last bit flipped of n. Consider,

00010100       &                (n = 20)
00010011                        (n-1 = 19)
~~~~~~~~
00010000
 
00…00000000    &                (n = 0, no rightmost bit)
11…11111111                     (n-1 = -1)
~~~~~~~~~~~
00…00000000

Download  Run Code

 
The following problems can be solved by unset the rightmost set bit of a number:

Problem 1. Check if a positive integer is a power of 2 without using any branching or loop.

As discussed above, the expression n & (n-1) will unset the rightmost set bit of a number. If the number is a power of 2, it has only a 1–bit set, and n & (n-1) will unset the only set bit. So, we can say that n & (n-1) returns 0 if n is a power of 2; otherwise, it’s not a power of 2.

For example,

00010000    &                (n = 16, only one set bit)
00001111                     (n-1 = 15)
~~~~~~~~
00000000

Download  Run Code

Problem 2. Find the position of the rightmost set bit

Practice this problem

The idea is to unset the rightmost bit of number n and XOR the result with n. Then the rightmost set bit in n will be the position of the only set bit in the result. Note that if n is odd, we can directly return 1 as the first bit is always set for odd numbers.

For example, the number 20 in binary is 00010100, and the position of the rightmost set bit is 3.

00010100    &                (n = 20)
00010011                     (n-1 = 19)
~~~~~~~~
00010000    ^                (XOR result number with n)
00010100
~~~~~~~~
00000100  ——  rightmost set bit will tell us the position

Following is the C++, Java, and Python implementation of the idea:

C++


Download  Run Code

Output:

20 in binary is 00010100
The position of the rightmost set bit is 3

Java


Download  Run Code

Output:

20 in binary is 10100
The position of the rightmost set bit is 3

Python


Download  Run Code

Output:

20 in binary is 0b10100
The position of the rightmost set bit is 3

Alternate Solution:

The idea is to negate n and perform bitwise AND operation with itself, i.e., n & -n. Then the position of the rightmost set bit in n will be the position of the only set bit in the result. We can also use this hack for problem 1. If (n & -n) == n, then the positive integer n is a power of 2.

For example,

00…0010100   &                    (n = 20)
11…1101100                        (-n = -20)
~~~~~~~~~~
00…0000100

Following is the C++, Java, and Python implementation of the idea:

C++


Download  Run Code

Output:

The position of the rightmost set bit is 3

Java


Download  Run Code

Output:

The position of the rightmost set bit is 3

Python


Download  Run Code

Output:

The position of the rightmost set bit is 3

Problem 3. Find the position of the only set bit in a number

The idea is to unset the rightmost bit of the number n and check if it becomes 0 or not. If it is non-zero, we know that there is another set bit present, and we have invalid input. If it becomes 0, then we can find the position of the only set bit by processing every bit of n one by one or directly using log2(n) + 1.

For example, the number 16 in binary is 00010000, and the position of the rightmost set bit is 5.

00010000    &                   (n = 16)
00001111                        (n-1 = 15)
~~~~~~~~
00000000
 
log2(16) + 1 = 5

Following is the C++, Java, and Python implementation of the idea:

C++


Download  Run Code

Output:

16 in binary is 00010000
The position of the only set bit is 5

Java


Download  Run Code

Output:

16 in binary is 10000
The position of the only set bit is 5

Python


Download  Run Code

Output:

16 in binary is 10000
The position of the only set bit is 5

Problem 4. Computing parity of a number

“Parity” refers to the total number of 1s in a binary number. The odd parity(1) means an odd number of 1s, and even parity(0) means an even number of 1s. Parity bits are often used as a simple means of error detection as digital data is transmitted and received.

Practice this problem

A naive solution is to calculate parity by checking each bit of n one by one. The time taken is proportional to the total number of bits in the number. The algorithm can be implemented as follows in C++, Java, and Python:

C++


Download  Run Code

Output:

31 in binary is 00011111
The parity of 31 is odd

Java


Download  Run Code

Output:

31 in binary is 11111
The parity of 31 is odd

Python


Download  Run Code

Output:

31 in binary is 11111
The parity of 31 is odd

We can perform better by turn off the rightmost set bit of the number one by one and count the parity. The following code uses an approach like Brian Kernighan’s bit counting. The time it takes is proportional to the total number of bits set.

Following is the C++, Java, and Python program that demonstrates it:

C++


Download  Run Code

Output:

31 in binary is 00011111
The parity of 31 is odd

Java


Download  Run Code

Output:

31 in binary is 11111
The parity of 31 is odd

Python


Download  Run Code

Output:

31 in binary is 0b11111
The parity of 31 is odd

 
Another Solution:

Compute the parity of a number using a lookup table

 
Also See:

Bit Hacks – Part 1 (Basic)
Bit Hacks – Part 2 (Playing with k’th bit)
Bit Hacks – Part 4 (Playing with letters of the English alphabet)
Bit Hacks – Part 5 (Find the absolute value of an integer without branching)

 
Suggested Read:

https://graphics.stanford.edu/~seander/bithacks.html