How to turn off a particular bit in a number?
Given a number n and a value k, turn off the k’th bit in n. Please note that k = 1 means the rightmost bit.
Input: n = 15, k = 1 Output: 14 Input: n = 14, k = 1 Output: 14 The rightmost bit was already off, so no change. Input: n = 15, k = 2 Output: 13 Input: n = 15, k = 3 Output: 11 Input: n = 15, k = 4 Output: 7 Input: n = 15, k >= 5 Output: 15
The idea is to use bitwise <<, & and ~ operators. Using expression “~(1 << (k – 1))“, we get a number which has all bits set, except the k’th bit. If we do bitwise & of this expression with n, we get a number which has all bits same as n except the k’th bit which is 0.
Below is the implementation of above idea.
Time Complexity: O(1)
Auxiliary Space: O(1)
Exercise: Write a function turnOnK() that turns the k’th bit on.
This article is contributed by Rahul Jain. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above