Given a number N, the task is to clear the K-th bit of this number N. If K-th bit is 1, then clear it to 0 and if it is 0 then leave it unchanged.

**Examples:**

Input:N = 5, K = 1Output:4 5 is represented as 101 in binary and has its first bit 1, so clearing it will result in 100 i.e. 4.Input:N = 5, K = 2Output:5 5 is represented as 101 in binary and has its second bit is already 0, so clearing it will result in 101 i.e. 5.

**Approach:**

- 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

Below is the implementation of the above approach:

## C

`// C program to clear K-th bit of a number N ` ` ` `#include <stdio.h> ` ` ` `// Function to clear the kth bit of n ` `int` `clearBit(` `int` `n, ` `int` `k) ` `{ ` ` ` `return` `(n & (~(1 << (k - 1)))); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 5, k = 1; ` ` ` ` ` `printf` `(` `"%d\n"` `, clearBit(n, k)); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to clear ` `# K-th bit of a number N ` ` ` `# Function to clear the kth bit of n ` `def` `clearBit(n, k): ` ` ` `return` `(n & ( ~(` `1` `<< (k ` `-` `1` `)))) ` ` ` `# Driver code ` `n ` `=` `5` `k ` `=` `1` ` ` `print` `(clearBit(n, k)) ` ` ` `# This code is contributed ` `# by Mohit Kumar ` |

*chevron_right*

*filter_none*

## Java

`// Java program to clear K-th bit of a number N ` `class` `GFG ` `{ ` ` ` ` ` `// Function to clear the kth bit of n ` ` ` `static` `int` `clearBit(` `int` `n, ` `int` `k) ` ` ` `{ ` ` ` `return` `(n & (~(` `1` `<< (k - ` `1` `)))); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main (String[] args) ` ` ` `{ ` ` ` `int` `n = ` `5` `, k = ` `1` `; ` ` ` ` ` `System.out.println(clearBit(n, k)); ` ` ` `} ` `} ` ` ` `// This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

## C#

`// C# program to clear K-th bit of a number N ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// Function to clear the kth bit of n ` ` ` `static` `int` `clearBit(` `int` `n, ` `int` `k) ` ` ` `{ ` ` ` `return` `(n & (~(1 << (k - 1)))); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main (String[] args) ` ` ` `{ ` ` ` `int` `n = 5, k = 1; ` ` ` ` ` `Console.WriteLine(clearBit(n, k)); ` ` ` `} ` `} ` ` ` `// This code is contributed by PrinciRaj1992 ` |

*chevron_right*

*filter_none*

**Output:**

4

Attention reader! Don't stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Set, Clear and Toggle a given bit of a number in C
- Queries for number of array elements in a range with Kth Bit Set
- Invert the Kth most significant bit of N
- Inserting m into n such that m starts at bit j and ends at bit i.
- Inserting M into N such that m starts at bit j and ends at bit i | Set-2
- Minimum bit flips such that every K consecutive bits contain at least one set bit
- Get the kth smallest number using the digits of the given number
- Program to toggle K-th bit of a number N
- Find the Kth number which is not divisible by N
- How to turn off a particular bit in a number?
- Toggle all the bits of a number except k-th bit.
- Toggling k-th bit of a number
- Set the K-th bit of a given number
- Remove one bit from a binary number to get maximum value
- Print 'K'th least significant bit of a number
- Find most significant set bit of a number
- Check if a number is positive, negative or zero using bit operators
- For every set bit of a number toggle bits of other
- How to turn on a particular bit in a number?
- Next greater number than N with exactly one bit different in binary representation of N

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.