Given a number N, the task is to set, clear and toggle the K-th bit of this number N.
- Setting a bit means that if K-th bit is 0, then set it to 1 and if it is 1 then leave it unchanged.
- Clearing a bit means that if K-th bit is 1, then clear it to 0 and if it is 0 then leave it unchanged.
- Toggling a bit means that if K-th bit is 1, then change it to 0 and if it is 0 then change it to 1.
Input: N = 5, K = 1 Output: Setting Kth bit: 5 Clearing Kth bit: 4 Toggling Kth bit: 4 Explanation: 5 is represented as 101 in binary and has its first bit 1, so setting it will result in 101 i.e. 5. clearing it will result in 100 i.e. 4. toggling it will result in 100 i.e. 4. Input: N = 7, K = 2 Output: Setting Kth bit: 7 Clearing Kth bit: 5 Toggling Kth bit: 5 Explanation: 7 is represented as 111 in binary and has its second bit 1, so setting it will result in 111 i.e. 7. clearing it will result in 101 i.e. 5. toggling it will result in 101 i.e. 5.
Below are the steps to set, clear and toggle Kth bit of N:
Setting a bit
- Since we all know that performing bitwise OR of any bit with a set bit results in a set bit, i.e.
Any bit <bitwise OR> Set bit = Set bit which means, 0 | 1 = 1 1 | 1 = 1
- So for setting a bit, performing a bitwise OR of the number with a set bit is the best idea.
N = N | 1 << K OR N |= 1 << K where K is the bit that is to be set
Clearing a bit
- Since bitwise AND of any bit with a reset bit results in a reset bit, i.e.
Any bit <bitwise AND> Reset bit = Reset bit which means, 0 & 0 = 0 1 & 0 = 0
- So for clearing a bit, performing a bitwise AND of the number with a reset bit is the best idea.
n = n & ~(1 << k) OR n &= ~(1 << k) where k is the bit that is to be cleared
Toggle a bit
- Since XOR of unset and set bit results in a set bit and XOR of a set and set bit results in an unset bit. Hence performing bitwise XOR of any bit with a set bit results in toggle of that bit, i.e.
Any bit <bitwise XOR> Set bit = Toggle which means, 0 ^ 1 = 1 1 ^ 1 = 0
- So in order to toggle a bit, performing a bitwise XOR of the number with a reset bit is the best idea.
n = n ^ 1 << k OR n ^= 1 << k where k is the bit that is to be cleared
Below is the implementation of the above approach:
5 with 1-th bit Set: 5 5 with 1-th bit Cleared: 4 5 with 1-th bit Toggled: 4
- Toggle all odd bits of a number
- Toggle first and last bits of a number
- Toggle all the bits of a number except k-th bit.
- Program to toggle K-th bit of a number N
- For every set bit of a number toggle bits of other
- Toggle all even bits of a number
- Program to clear K-th bit of a number N
- Toggle bits of a number except first and last bits
- Toggle the last m bits
- Toggle bits in the given range
- Toggle all bits after most significant bit
- Toggle case of a string using Bitwise Operators
- Count number of trailing zeros in Binary representation of a number using Bitset
- Maximize a given unsigned number number by swapping bits at it's extreme positions.
- Check if the binary representation of a number has equal number of 0s and 1s in blocks
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.