This post will discuss a few related problems that operate on the k’th bit of a number.

The following problems are covered in this post:

Problem 1. Turn off k’th bit in a number

Practice this problem

The idea is to use bitwise <<, &, and ~ operators. Using the expression ~ (1 << (k - 1)), we get a number with all its bits set, except the k'th bit. If we do a bitwise AND of this expression with n, i.e., n & ~(1 << (k - 1)), we get a number which has all bits the same as n except the k'th bit which will be set to 0.

For example, consider n = 20 and k = 3.

00010100    &                 (n = 20)
11111011                    ~ (1 << (3-1))
~~~~~~~~
00010000

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

C++


Download  Run Code

Output:

20 in binary is 00010100
Turning k’th bit off
16 in binary is 00010000

Java


Download  Run Code

Output:

20 in binary is 10100
Turning k’th bit off…
16 in binary is 10000

Python


Download  Run Code

Output:

20 in binary is 0b10100
Turning k’th bit off…
16 in binary is 0b10000

Problem 2. Turn on k’th bit in a number

Practice this problem

The idea is to use bitwise << and | operators. Using the expression 1 << (k - 1), we get a number with all bits 0, except the k'th bit. If we do bitwise OR of this expression with n, i.e., n | (1 << (k - 1)), we get a number which has all bits the same as n except the k'th bit which will be set to 1.

For example, consider n = 20 and k = 4.

00010100    |               (n = 20)
00001000                    (1 << (4 – 1))
~~~~~~~~
00011100

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

C++


Download  Run Code

Output:

20 in binary is 00010100
Turning k’th bit on
28 in binary is 00011100

Java


Download  Run Code

Output:

20 in binary is 10100
Turning k’th bit on…
28 in binary is 11100

Python


Download  Run Code

Output:

20 in binary is 0b10100
Turning k’th bit on…
28 in binary is 0b11100

Problem 3. Check if k’th bit is set for a number

Practice this problem

The idea is to use bitwise << and & operators. Using the expression 1 << (k - 1), we get a number with all bits 0, except the k'th bit. If we do bitwise AND of this expression with n, i.e., n & (1 << (k - 1)), any non-zero value indicates that its k'th bit is set.

For example, consider n = 20 and k = 3.

00010100    &               (n = 20)
00000100                    (1 << (3-1))
~~~~~~~~
00000100                    non-zero value

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

C++


Download  Run Code

Output:

20 in binary is 00010100
k’th bit is set

Java


Download  Run Code

Output:

20 in binary is 10100
k’th bit is set

Python


Download  Run Code

Output:

20 in binary is 0b10100
k’th bit is set

Problem 4. Toggle the k’th bit

Practice this problem

The idea is to use bitwise ^ and << operators. By using the expression 1 << (k - 1), we get a number with all bits 0, except the k'th bit. If we do bitwise XOR of this expression with n, i.e., n ^ (1 << k), we can easily toggle its k'th bit as 0 ^ 1 = 1 and 1 ^ 1 = 0.

This approach is demonstrated below in C++, Java, and Python:

C++


Download  Run Code

Output:

20 in binary is 00010100
Toggling k’th bit of n
16 in binary is 00010000

Java


Download  Run Code

Output:

20 in binary is 10100
Toggling k’th bit of n…
16 in binary is 10000

Python


Download  Run Code

Output:

20 in binary is 0b10100
Toggling k’th bit of n…
16 in binary is 0b10000

 
Also See:

Bit Hacks – Part 1 (Basic)
Bit Hacks – Part 3 (Playing with the rightmost set bit of a number)
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