Toggling k-th bit of a number - GeeksforGeeks

24 Nov, 2021
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 the 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++
Java
Python3
C#
PHP
Javascript
// 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 codeint main(){    int n = 5, k = 1;    cout << toggleKthBit(n , k);    return 0;}
// Java program to toggle// 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));    }}
# Python3 code to toggle k-th bit of n def toggleKthBit(n, k):    return (n ^ (1 << (k-1)))     # Driver coden = 5k = 1print( toggleKthBit(n , k)) # This code is contributed by "Sharad_Bhardwaj".
// C# program to toggle// k-th bit of a numberusing 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.
<?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?>
<script>     // Javascript program to toggle    // k-th bit of a number         function toggleKthBit(n, k)    {        return (n ^ (1 << (k-1)));    }         let n = 5, k = 1;                 document.write(toggleKthBit(n , k));     </script>
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 write.geeksforgeeks.org or mail your article to review-team@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. 
jit_t
divyesh072019
abhishek0719kadiyan
Bit Magic
Bit Magic
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Set, Clear and Toggle a given bit of a number in C
Write an Efficient Method to Check if a Number is Multiple of 3
Highest power of 2 less than or equal to given number
Swap bits in a given number
Check for Integer Overflow
Reverse actual bits of the given number
Bit Tricks for Competitive Programming
Count total bits in a number
Count number of bits to be flipped to convert A to B
Program to find parity