Toggle all the bits of a number except k-th bit. Last Updated : 17 Mar, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 get 1. Input : n = 1, k = 1 Output : 4294967292 4294967262 has all bits toggled except second bit from right.Toggle bit at k-th position. We do it by finding a number with only k-th bit set (using 1 << k), then doing bitwise XOR of this number n.Toggle all bits of number obtained above using ~ (Bitwise negation) C++ // C++ program to toggle all bits except kth bit #include <iostream> using namespace std; // Returns a number with all bit toggled in n // except k-th bit unsigned int toggleAllExceptK(unsigned int n, unsigned int k) { /* 1) Toggle k-th bit by doing n ^ (1 << k) 2) Toggle all bits of the modified number */ return ~(n ^ (1 << k)); } // Driver code int main() { unsigned int n = 4294967295; unsigned int k = 0; cout << toggleAllExceptK(n, k); return 0; } // This code is contributed by khushboogoyal499 C // C program to toggle all bits except kth bit #include<stdio.h> // Returns a number with all bit toggled in n // except k-th bit unsigned int toggleAllExceptK(unsigned int n, unsigned int k) { /* 1) Toggle k-th bit by doing n ^ (1 << k) 2) Toggle all bits of the modified number */ return ~(n ^ (1 << k)); } // Driver code int main() { unsigned int n = 4294967295; unsigned int k = 0; printf("%u", toggleAllExceptK( n, k)); return 0; } Java public class Main { // Returns a number with all bit toggled in n // except k-th bit public static long toggleAllExceptK(long n, int k) { // 1) Toggle k-th bit by doing n ^ (1 << k) // 2) Toggle all bits of the modified number long temp = n ^ (1L << k); long mask = (1L << 32) - 1; return ~temp & mask; } // Driver code public static void main(String[] args) { long n = 4294967295L; int k = 0; System.out.println(toggleAllExceptK(n, k)); } } Python3 # Python3 program to toggle all bits # except kth bit # Returns a number with all bit toggled # in n except k-th bit def toggleAllExceptK(n, k): # 1) Toggle k-th bit by doing n ^ (1 << k) # 2) Toggle all bits of the modified number temp = bin(n ^ (1 << k))[2:] ans = "" for i in temp: if i == '1': ans += '0' else: ans += '1' return int(ans, 2) # Driver code if __name__ == '__main__': n = 4294967295 k = 0 print(toggleAllExceptK(n, k)) # This code is contributed by mohit kumar 29 JavaScript <script> // javascript program to toggle all bits except kth bit // Returns a number with all bit toggled in n // except k-th bit function toggleAllExceptK(n,k) { /* 1) Toggle k-th bit by doing n ^ (1 << k) 2) Toggle all bits of the modified number */ return ~(n ^ (1 << k)); } // Driver code let n = 4294967295; let k = 0; document.write(toggleAllExceptK(n, k)); //This code is contributed by unknown2108 </script> C# // C# equivalent code using System; public class Program { // Returns a number with all bit toggled in n // except k-th bit public static long toggleAllExceptK(long n, int k) { // 1) Toggle k-th bit by doing n ^ (1 << k) long temp = n ^ (1L << k); // 2) Toggle all bits of the modified number long mask = (1L << 32) - 1; return ~temp & mask; } // Driver code public static void Main(string[] args) { long n = 4294967295L; int k = 0; Console.WriteLine(toggleAllExceptK(n, k)); } } Output: 1 Time Complexity : O(1) Space Complexity : O(1) Comment More infoAdvertise with us Next Article Program to toggle K-th bit of a number N K kartik Follow Improve Article Tags : Bit Magic DSA Practice Tags : Bit Magic Similar Reads Toggle bits of a number except first and last bits Given a number, the task is to toggle bits of the number except the first and the last bit.Examples: Input : 10 Output : 12 Binary representation:- 1 0 1 0 After toggling first and last : 1 1 0 0 Input : 9 Output : 15 Binary representation : 1 0 0 1 After toggling first and last : 1 1 1 1 Prerequisi 8 min read Set the K-th bit of a given number Given a number n and a value k. From the right, set the kth bit in the binary representation of n. The position of LSB(or last bit) is 0, second last bit is 1 and so on. Also, 0 <= k < x, where x is the number of bits in the binary representation of n.Examples: Input : n = 10, k = 2 Output : 1 4 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 Program to toggle 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 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 3 min read Check if all bits of a number are set Given a number n. The problem is to check whether every bit in the binary representation of the given number is set or not. Here 0 <= n.Examples : Input : 7Output : Yes(7)10 = (111)2Input : 14Output : NoMethod 1: If n = 0, then answer is 'No'. Else perform the two operations until n becomes 0. Wh 11 min read Set all the bits in given range of a number Given a non-negative number n and two values l and r. The problem is to set the bits in the range l to r in the binary representation of n, i.e, to unset bits from the rightmost lth bit to the rightmost r-th bit. Constraint: 1 <= l <= r <= number of bits in the binary representation of n.Ex 5 min read Like