For a given number n, if k-th bit is 0, then toggle it to 1 and if it is 1 then, toggle it to 0.

**Examples :**

Input : n = 5, k = 1 Output : 4 5 is represented as 101 in binary and has its first bit 1, so toggling it will result in 100 i.e. 4. Input : n = 2, k = 3 Output : 6 Input : n = 75, k = 4 Output : 67

Below are simple steps to find value of k-th bit

1) Left shift given number 1 by k-1 to create a number that has only set bit as k-th bit. temp = 1 << (k-1) 2) Return bitwise XOR of temp and n. Since temp has only k-th bit set, doing XOR would toggle only this bit.

**Example : **

n = 75 and k = 4 temp = 1 << (k-1) = 1 << 3 = 8 Binary Representation of temp = 0..00001000 Binary Representation of n = 0..01001011 Bitwise XOR of two numbers = 0..01000011

## C++

`// CPP program to toggle k-th bit of n ` `#include<iostream> ` `using` `namespace` `std; ` ` ` `int` `toggleKthBit(` `int` `n, ` `int` `k) ` `{ ` ` ` `return` `(n ^ (1 << (k-1))); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 5, k = 1; ` ` ` `cout << toggleKthBit(n , k); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

` ` `// Java program to toogle ` `// k-th bit of a number ` ` ` `class` `Toggle ` `{ ` ` ` `static` `int` `toggleKthBit(` `int` `n, ` `int` `k) ` ` ` `{ ` ` ` `return` `(n ^ (` `1` `<< (k-` `1` `))); ` ` ` `} ` ` ` ` ` `// main function ` ` ` `public` `static` `void` `main (String[] args) ` ` ` `{ ` ` ` `int` `n = ` `5` `, k = ` `1` `; ` ` ` `System.out.println(toggleKthBit(n , k)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 code to toggle k-th bit of n ` ` ` `def` `toggleKthBit(n, k): ` ` ` `return` `(n ^ (` `1` `<< (k` `-` `1` `))) ` ` ` `# Driver code ` `n ` `=` `5` `k ` `=` `1` `print` `( toggleKthBit(n , k)) ` ` ` `# This code is contributed by "Sharad_Bhardwaj". ` |

*chevron_right*

*filter_none*

## C#

`// C# program to toogle ` `// k-th bit of a number ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `static` `int` `toggleKthBit(` `int` `n, ` `int` `k) ` ` ` `{ ` ` ` `return` `(n ^ (1 << (k-1))); ` ` ` `} ` ` ` ` ` `// main function ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `n = 5, k = 1; ` ` ` ` ` `Console.WriteLine(toggleKthBit(n , k)); ` ` ` `} ` `} ` ` ` `//This code is contributed by Anant Agarwal. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// Php program to toggle k-th bit of n ` ` ` `function` `toggleKthBit(` `$n` `, ` `$k` `) ` `{ ` ` ` `return` `(` `$n` `^ (1 << (` `$k` `- 1))); ` `} ` ` ` `// Driver code ` `$n` `= 5; ` `$k` `= 1; ` `echo` `toggleKthBit(` `$n` `, ` `$k` `); ` ` ` `// This code is contributed by Ajit ` `?> ` |

*chevron_right*

*filter_none*

**Output :**

4

This article is contributed by **SAKSHI TIWARI**. If you like GeeksforGeeks(We know you do!) 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

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:

- Maximum Sum of Products of two arrays by toggling adjacent bits
- 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
- 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
- Smallest submatrix with Kth maximum XOR
- Find the value at kth position in the generated array
- Find Kth smallest value for b such that a + b = a | b
- Kth array element after M replacements of array elements by XOR of adjacent pairs
- Sum of maximum and minimum of Kth subset ordered by increasing subset sum
- How to turn off a particular bit in a number?
- Toggle all the bits of a number except k-th bit.
- 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