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

**Examples:**

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

**Approach:**

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

## C

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

*chevron_right*

*filter_none*

## Java

`// Java program to toggle K-th bit of a number N ` `class` `GFG ` `{ ` ` ` `// Function to toggle the kth bit of n ` `static` `int` `toggleBit(` `int` `n, ` `int` `k) ` `{ ` ` ` `return` `(n ^ (` `1` `<< (k - ` `1` `))); ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String []args) ` `{ ` ` ` `int` `n = ` `5` `, k = ` `2` `; ` ` ` ` ` `System.out.printf(` `"%d\n"` `, toggleBit(n, k)); ` `} ` `} ` ` ` `// This code is contributed by Rajput-Ji ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to clear K-th bit ` `# of a number N ` ` ` `# Function to toggle the kth bit of n ` `def` `toggleBit(n, k) : ` ` ` ` ` `return` `(n ^ (` `1` `<< (k ` `-` `1` `))); ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `n ` `=` `5` `; k ` `=` `2` `; ` ` ` ` ` `print` `(toggleBit(n, k)); ` ` ` `# This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

## C#

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

*chevron_right*

*filter_none*

**Output:**

7

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:

- Toggle all the bits of a number except k-th bit.
- For every set bit of a number toggle bits of other
- Set, Clear and Toggle a given bit of a number in C
- Toggle all bits after most significant bit
- Queries for number of array elements in a range with Kth Bit Set
- Smallest number exceeding N whose Kth bit is set
- Invert the Kth most significant bit of N
- Toggle first and last bits of a number
- Toggle all even bits of a number
- Toggle all odd bits of a number
- Toggle bits of a number except first and last bits
- 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
- Toggle case of a string using Bitwise Operators
- Toggle the last m bits
- Toggle bits in the given range
- Get the kth smallest number using the digits of the given number
- Program to clear K-th bit of a number N
- Find the Kth number which is not divisible by 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.