# 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.

Examples:

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.

## C++

`#include <iostream> ` `using` `namespace` `std; ` ` ` `// Returns a number that has all bits same as n ` `// except the k'th bit which is made 0 ` `int` `turnOffK(` `int` `n, ` `int` `k) ` `{ ` ` ` `// k must be greater than 0 ` ` ` `if` `(k <= 0) ` `return` `n; ` ` ` ` ` `// Do & of n with a number with all set bits except ` ` ` `// the k'th bit ` ` ` `return` `(n & ~(1 << (k - 1))); ` `} ` ` ` `// Driver program to test above function ` `int` `main() ` `{ ` ` ` `int` `n = 15; ` ` ` `int` `k = 4; ` ` ` `cout << turnOffK(n, k); ` ` ` `return` `0; ` `}` |

*chevron_right*

*filter_none*

## Java

`// Java program to turn off a particular bit in a number ` `import` `java.io.*; ` ` ` `class` `TurnOff ` `{ ` ` ` `// Function to returns a number that has all bits same as n ` ` ` `// except the k'th bit which is made 0 ` ` ` `static` `int` `turnOffK(` `int` `n, ` `int` `k) ` ` ` `{ ` ` ` `// k must be greater than 0 ` ` ` `if` `(k <= ` `0` `) ` ` ` `return` `n; ` ` ` ` ` `// Do & of n with a number with all set bits except ` ` ` `// the k'th bit ` ` ` `return` `(n & ~(` `1` `<< (k - ` `1` `))); ` ` ` `} ` ` ` ` ` `// Driver program ` ` ` `public` `static` `void` `main (String[] args) ` ` ` `{ ` ` ` `int` `n = ` `15` `; ` ` ` `int` `k = ` `4` `; ` ` ` `System.out.println(turnOffK(n, k)); ` ` ` `} ` `} ` `// Contributed by Pramod Kumar ` |

*chevron_right*

*filter_none*

## Python3

`# Returns a number that ` `# has all bits same as n ` `# except the k'th bit ` `# which is made 0 ` ` ` `def` `turnOffK(n,k): ` ` ` ` ` `# k must be greater than 0 ` ` ` `if` `(k <` `=` `0` `): ` ` ` `return` `n ` ` ` ` ` `# Do & of n with a number ` ` ` `# with all set bits except ` ` ` `# the k'th bit ` ` ` `return` `(n & ~(` `1` `<< (k ` `-` `1` `))) ` ` ` ` ` `# Driver code ` `n ` `=` `15` `k ` `=` `4` `print` `(turnOffK(n, k)) ` ` ` `# This code is contributed ` `# by Anant Agarwal. ` |

*chevron_right*

*filter_none*

## C#

`// C# program to turn off a ` `// particular bit in a number ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// Function to returns a number ` ` ` `// that has all bits same as n ` ` ` `// except the k'th bit which is ` ` ` `// made 0 ` ` ` `static` `int` `turnOffK(` `int` `n, ` `int` `k) ` ` ` `{ ` ` ` `// k must be greater than 0 ` ` ` `if` `(k <= 0) ` ` ` `return` `n; ` ` ` ` ` `// Do & of n with a number ` ` ` `// with all set bits except ` ` ` `// the k'th bit ` ` ` `return` `(n & ~ (1 << (k - 1))); ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `Main () ` ` ` `{ ` ` ` `int` `n = 15; ` ` ` `int` `k = 4; ` ` ` `Console.Write(turnOffK(n, k)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Nitin Mittal. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to turn off a ` `// particular bit in a number ` ` ` `// Returns a number that has ` `// all bits same as n except ` `// the k'th bit which is made 0 ` `function` `turnOffK(` `$n` `, ` `$k` `) ` `{ ` ` ` ` ` `// k must be greater than 0 ` ` ` `if` `(` `$k` `<= 0) ` ` ` `return` `$n` `; ` ` ` ` ` `// Do & of n with a number ` ` ` `// with all set bits except ` ` ` `// the k'th bit ` ` ` `return` `(` `$n` `& ~(1 << (` `$k` `- 1))); ` `} ` ` ` `// Driver Code ` `$n` `= 15; ` `$k` `= 4; ` `echo` `turnOffK(` `$n` `, ` `$k` `); ` ` ` `// This code is contributed by nitin mittal ` `?> ` |

*chevron_right*

*filter_none*

Output:

7

**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

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- How to turn on a particular bit in a number?
- Turn off the rightmost set bit | Set 2
- Turn off the rightmost set bit
- Find the largest number smaller than integer N with maximum number of set bits
- 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.
- Number of ways to split a binary number such that every part is divisible by 2
- Given a number as a string, find the number of contiguous subsequences which recursively add up to 9 | Set 2
- Check if the binary representation of a number has equal number of 0s and 1s in blocks
- Find a number which give minimum sum when XOR with every number of array of integers
- Given a number as a string, find the number of contiguous subsequences which recursively add up to 9
- Build Lowest Number by Removing n digits from a given number
- Number of leading zeros in binary representation of a given number
- Minimum number of given powers of 2 required to represent a number
- Check if a large number is divisible by a number which is a power of 2
- Number of substrings with length divisible by the number of 1's in it
- Program to Convert BCD number into Decimal number
- Largest even number possible by using one swap operation in given number
- Maximum number of splits of a binary number
- Minimize the number of replacements to get a string with same number of 'a', 'b' and 'c' in it