Tuesday, June 9, 2026
HomeData Modelling & AIToggle all the bits of a number except k-th bit.

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 get 1.

Input  :  n = 1, k = 1
Output : 4294967292
4294967262 has all bits toggled except second
bit from right.
  1. 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.
  2. 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)

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

1 COMMENT

Most Popular

Dominic
32515 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6896 POSTS0 COMMENTS
Nicole Veronica
12012 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12109 POSTS0 COMMENTS
Shaida Kate Naidoo
7018 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6976 POSTS0 COMMENTS
Umr Jansen
6963 POSTS0 COMMENTS