Program to toggle K-th bit of a number N Last Updated : 08 Nov, 2021 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 = 2 Output: 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 = 1 Output: 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 = 0So 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<bits/stdc++.h> using namespace std; // 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; cout << toggleBit(n, k) << endl; return 0; } // This code is contributed by noob2000 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; } 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 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 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 JavaScript <script> // Javascript program to toggle K-th bit of a number N // Function to toggle the kth bit of n function toggleBit(n, k) { return (n ^ (1 << (k - 1))); } // Driver code var n = 5, k = 2; document.write( toggleBit(n, k)); // This code is contributed by famously. </script> Output: 7 Time Complexity: O(1) Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Write an Efficient C Program to Reverse Bits of a Number C code_r Follow Improve Article Tags : Bit Magic DSA Numbers Practice Tags : Bit MagicNumbers Similar Reads Program to clear K-th bit of a number N 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 = 1 Output: 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, 3 min read Toggling k-th bit of a number 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 = 6, k = 1Output : 76 is represented as 110 in binary and has its first bit 0, so toggling it will result in 111 i.e. 7.Input : n = 2, k = 3Output : 62 is represented as 010 in binary 3 min read For every set bit of a number toggle bits of other Given two integer numbers, whenever the bits of the first number is set i.e. 1, toggle the bits of the second number leaving the rest bits of the second number unchanged. Examples : Input: 2 5 Output: 7 2 is represented as 10 in binary and 5 is represented as 101. Hence toggling the 2nd bit of 5 fro 3 min read Write an Efficient C Program to Reverse Bits of a Number Given an unsigned integer, reverse all bits of it and return the number with reversed bits. Input : n = 1Output : 2147483648 Explanation : On a machine with size of unsigned bit as 32. Reverse of 0....001 is 100....0. Input : n = 2147483648Output : 1 Recommended PracticeReverse BitsTry It!Method1 - 6 min read Position of the K-th set bit in a number Given two numbers N and K, The task is to find the index of the K-th set bit in the number from the right. Note: Indexing in the binary representation starts from 0 from the right. For example in the binary number "000011", the first set bit is at index 0 from the right, and the second set bit is at 6 min read Toggle all the bits of a number except k-th bit. Given a positive (or unsigned) integer n, write a function to toggle all the bits except k-th bit. Here value of k starts from 0 (zero) and from right. Examples: Input : n = 4294967295, k = 0 Output : 1 The number 4294967295 in 32 bits has all bits set. When we toggle all bits except last bit, we ge 4 min read Like