Toggle all the bits of a number except k-th bit

In this blog post, I will teach you how to write a C/C++ program to toggle all the bits of a number except kth bit. After reading this detailed guide post I believe you can write an efficient program to toggle all the bits of a number except the kth bit.

Consider the examples below for a better understanding,

Example-1:

Input : n = 5


Here's a simple ASCII representation of 5 (an 8-bit integer):
  7   6   5   4   3   2   1   0    (Bit position)
+---+---+---+---+---+---+---+---+
| 0 | 0 | 0 | 0 | 0 | 1 | 0 | 1 |  (Example bit values)
+---+---+---+---+---+---+---+---+
                  ^
                  |
                 Want to toggle all bits except this Bit (3rd bit)


(After toggling bit all bits except 3rd bit) 


Output: 242

  7   6   5   4   3   2   1   0    (Bit position)
+---+---+---+---+---+---+---+---+
| 1 | 1 | 1 | 1 | 0 | 0 | 1 | 0 |  (Example bit values)
+---+---+---+---+---+---+---+---+
                  ^
                  |
                 You can see bit has not been toggled.

 

Example-2:

Input : n = 5


Here's a simple ASCII representation of 13 (an 8-bit integer):

  7   6   5   4   3   2   1   0    (Bit position)
+---+---+---+---+---+---+---+---+
| 0 | 0 | 0 | 0 | 1 | 1 | 0 | 1 |  (Example bit values)
+---+---+---+---+---+---+---+---+
                      ^
                      |
                      Want to toggle all bits except this Bit (2nd bit)



(After toggling bit all bits except 2nd bit) 



Output: 246

  7   6   5   4   3   2   1   0    (Bit position)
+---+---+---+---+---+---+---+---+
| 1 | 1 | 1 | 1 | 0 | 1 | 1 | 0 |  (Example bit values)
+---+---+---+---+---+---+---+---+
                      ^
                      |
                      You can see bit has not been toggled.

 

 

Let’s see the problem statements for this question.

Problem Statement: Given a positive integer n, write a function to toggle all the bits except kth bit.  Note that k = 0 means the rightmost bit of ‘n’.

Disclaimer: Try to solve the problem yourself first otherwise it would be not worth solving this problem.

 

Using ^ and ~ operators with a BitMask:

Using Bitwise EX-OR operator (^) and complement operator (~) With the help of a Mask you can easily toggle all the bits except the kth bit. In this approach first, we will create a MASK using the expression (1 << d).

After creating the mask, you need to perform the bitwise EX_OR between the given number ‘n’ and the MASK it toggles the kth bit.

Now in the last toggling all bits of the number that gets by the above operation using the complement operator, i.e., transforming the 0 bit to 1 and the 1 bit to 0.

Let’s take an example to understand the above steps, suppose n = 5.

Now if you want to set its 3rd bit (bit-position start from 0), the mask would be (1<<3).

Mask: (1 << 3)


  7   6   5   4   3   2   1   0    (Bit position)
+---+---+---+---+---+---+---+---+
| 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 |  (Example bit values)
+---+---+---+---+---+---+---+---+

 

Now Ex-Oring this mask with the original number ‘n’.

Original number n: 5
                   00000101 (5 in binary)
                    ^
Mask:              00001000

Result: =          00001101

 

Now toggle all bits of the results.

//Toggle all bits of the get result 

 ~ (00001101)


Output: 11110010

 

C program to toggle all the bits of a number except k-th bit:

/*
C program to Toggle all the bits
of a number except k-th bit.
*/

#include <stdio.h>

/*If k valid, then returns a number
 with all bit toggled in n except k-th bit
*/
unsigned int toggleAllBitsExceptK(unsigned int n, int k)
{
    unsigned int tmpN = n;
    unsigned int MASK = 0;
    //d must be greater than 0
    if(k >= 0)
    {
        //Create MASK
        MASK = (1 << k);

        //Toggle k-th bit by doing n ^ MASK

        tmpN ^= MASK;


        //Toggle all bits of the modified number
        tmpN = ~tmpN;

    }
    else
    {
        printf("Enter valid value of position\n");
    }
    return tmpN;
}



int main()
{
    unsigned int n = 4294967295; //0xffffffff
    
    //toggle all bits except 0th bit
    int k = 0;

    const unsigned int v = toggleAllBitsExceptK(n, k);

    printf("v = %u", v);

    return 0;
}

Output: v = 1

 

C++ program to toggle all the bits of a number except k-th bit:

/*
C++ program to Toggle all the bits
of a number except k-th bit.
*/
#include <iostream>


/*If k valid, then returns a number
 with all bit toggled in n except k-th bit
*/
unsigned int toggleAllBitsExceptK(unsigned int n, int k)
{
    unsigned int tmpN = n;
    unsigned int MASK = 0;
    //d must be greater than 0
    if(k >= 0)
    {
        //Create MASK
        MASK = (1 << k);
        //Toggle k-th bit by doing n ^ MASK
        tmpN ^= MASK;
        //Toggle all bits of the modified number
        tmpN = ~tmpN;
    }
    else
    {
        std::cout<<"Enter valid value of position\n"<<std::endl;
    }
    return tmpN;
}


int main()
{
    unsigned int n = 4294967295; //0xffffffff

    //toggle all bits except 0th bit
    int k = 0;

    const unsigned int v = toggleAllBitsExceptK(n, k);

    std::cout<<"Toggle All Bits Except "<<k<<std::endl;

    std::cout<<"Value "<<v<<std::endl;

    return 0;
}

Output:

Toggle All Bits Except 0
Value 1

 

Recommended Post

Leave a Reply

Your email address will not be published. Required fields are marked *