How to turn off a particular bit in a number: A Step-by-Step Guide

In this blog post, I will teach you how to write C/C++ program to turn off a particular bit in a number. After reading this detail guide post I believe you able to write an efficient program to turn off a particular bit in a number.

Consider the below examples,

Example-1:

Input : n = 9

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

  7   6   5   4   3   2   1   0    (Bit position)
+---+---+---+---+---+---+---+---+
| 0 | 0 | 0 | 0 | 1 | 0 | 0 | 1 |  (Example bit values)
+---+---+---+---+---+---+---+---+
                  ^
                  |
                 Want to clear this Bit (3rd bit)



Output: 1

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

 

Example-2:

Input : n = 13

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 turn off this Bit (2nd bit)



Output: 9

  7   6   5   4   3   2   1   0    (Bit position)
+---+---+---+---+---+---+---+---+
| 0 | 0 | 0 | 0 | 1 | 0 | 0 | 1 |  (Example bit values)
+---+---+---+---+---+---+---+---+
                      ^
                      |
                     Now bit has been turned off.

 

 

Let’s see the problem statements for this question.

Problem Statement: Given a number ‘n‘ and a value ‘d‘, turn off the dth bit in ‘n’. You can see in the binary representation bit position start from the 0th, so please note that d = 0 means the rightmost bit of ‘n’.

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

 

1. Using Bitwise AND (&) with a Bit Mask:

This approach is one of most popular way to turn off any bits of a given number. In this approach we use a bit mask to clear the bit at the specified position.

🤔 Now you will think how to create a bit mask.

Don’t worry I will explain. 🤗

Using the expression (1 << d), you can create a mask. This mask has only a set bit which you want to turn off.

Now complements this mask ~(1 << d). It will set all bits except the dth bits which you want to clear.

At the last performing bitwise AND with this mask clears the dth bit of given number n.

 

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

Now if you want to clear its second bit (as I have described in the beginning of the article that the rightmost bit is the 0th bit), so here mask would be (1<<2).

Mask: (1 << 2)

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

 

Now complements this mask ~(1<< 2), this would 11111011.

Mask: ~(1 << 2)


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

 

Now ANDing this mask with original number ‘n’.

Original number n: 00001101
                    &
Mask:              11111011


Result: =        00001001

 

C program to turn off a particular bit in a number:

 

/*
C program to turn off a
particular bit in a number
*/
#include <stdio.h>

//If d valid, then returns a number
// that has all bits same as n
// except its d'th bit which is turn off
unsigned int turnOffDthBits(unsigned int n, int d)
{
    unsigned int tmpN = n ;
    //d must be greater than 0
    if(d > 0)
    {
        tmpN = (n & (~(1 << d)));
    }
    else
    {
        printf("Enter valid value of position\n");
    }

    return tmpN;
}

int main()
{
    int n = 13;
    int d = 2;

    const unsigned int v = turnOffDthBits(n, d);

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

    return 0;
}





Output: 9

 

C++ program to turn off a particular bit in a number:

/*
C++ program to turn off a
particular bit in a number
*/
#include <iostream>

//If d valid, then returns a number
// that has all bits same as n
// except its d'th bit which is turn off
unsigned int turnOffDthBits(unsigned int n, int d)
{
    unsigned int tmpN = n ;
    //d must be greater than 0
    if(d > 0)
    {
        tmpN = (n & (~(1 << d)));
    }
    else
    {
        std::cout<<"Enter valid value of position"<<std::endl;
    }

    return tmpN;
}

int main()
{
    int n = 13;
    int d = 2;

    std::cout << turnOffDthBits(n, d);

    return 0;
}





Output: 9

 

2. Using Bitwise XOR (^) with a Bit Mask:

This approach approximately uses the same concept what we have implemented in the above example including the concept of XOR operator.

First, we need to check the bit which want to clear or turn off. If the bit is set, then do the XOR with mask for togging the dth bit.

C program to clear a particular bit in a number:

/*
C program to turn off a
particular bit in a number
*/
#include <stdio.h>

//If d valid, then returns a number
//that has all bits same as n
//except its d'th bit which is turn off
unsigned int turnOffDthBits(unsigned int n, int d)
{
    unsigned int tmpN = n;
    unsigned int MASK = 0;

    //d must be greater than 0
    if(d > 0)
    {
        //Create MASK
        MASK = (1 << d);

        //if dth-bit set,then toggle the dth bit
        tmpN ^= (n & MASK)? MASK:0U;
    }
    else
    {
        printf("Enter valid value of position\n");
    }

    return tmpN;
}

int main()
{
    int n = 13;
    int k = 2;

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

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

    return 0;
}

Output: 9

 

C++ program to reset a particular bit in a number:

/*
C++ program to turn off a
particular bit in a number
*/
#include <iostream>

//If d valid, then returns a number
// that has all bits same as n
// except its d'th bit which is turn off
unsigned int turnOffDthBits(unsigned int n, int d)
{
    unsigned int tmpN = n;
    unsigned int MASK = 0;

    //d must be greater than 0
    if(d > 0)
    {
        //Create MASK
        MASK = (1 << d);

        //if dth-bit set,then toggle the dth bit
        tmpN ^= (n & MASK)? MASK:0U;
    }
    else
    {
        std::cout<<"Enter valid value of position"<<std::endl;
    }

    return tmpN;
}

int main()
{
    int n = 13;
    int k = 2;

    std::cout << turnOffDthBits(n, k);

    return 0;
}

Output: 9

 

Recommended Post

Leave a Reply

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