How to Set 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 set a particular bit in a number. After reading this detail guide post I believe you able to write an efficient program to set bit in a number.

Consider the below examples,

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 set this Bit (3rd bit)




Output: 13

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

 

Example-2:

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 set this Bit (1st bit)




Output: 11

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

 

Let’s see the problem statements for this question.

Problem Statement: Given a number ‘n‘ and a value ‘d‘, set 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 OR (|) with a Bit Mask:

With the help of Bitwise OR (|) and a Mask you can easily setting the bits of a given number. In this approach we use a bit mask to set the bit at the specified position.

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

At the last performing bitwise OR with this mask set the dth bit of given number n.

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 the 0), so here 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 Oring this mask with original number ‘n’.

Original number n: 5

                   00000101 (5 in binary)
                    |
Mask:              00001000

Result: =          00001101

 

C program to set a particular bit in a number:

/*
C program to set 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 set
//if it was not set
unsigned int setDthBits(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 = 5;
    int d = 3;

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

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

    return 0;
}

Output: 13

 

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

 

/*
C++ program to set 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 set
//if it was not set
unsigned int setDthBits(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 = 5;
    int d = 3;

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

    return 0;
}

Output: 13

 

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

Using the XOR Operator you can also set the specific bits of a number. But in this approach, you have to first check the bit which you want to set. If the bit is not set, then do the XOR with mask for toggling the dth bit.

 

C program to set dth bit in a number:

/*
C  program to set 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 set
//if it was not set
unsigned int setDthBits(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 not set,then toggle the dth bit
        tmpN ^= (n & MASK)? 0U : MASK;
    }
    else
    {
        printf("Enter valid value of position\n");
    }
    return tmpN;
}


int main()
{
    int n = 9;
    int d = 1;

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

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

    return 0;
}

Output: 11

 

C++ program to on specific bit in a number:

/*
C++ program to set 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 set
//if it was not set
unsigned int setDthBits(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 not set,then toggle the dth bit
        tmpN ^= (n & MASK)? 0U : MASK;
    }
    else
    {
        std::cout<<"Enter valid value of position"<<std::endl;
    }
    return tmpN;
}



int main()
{
    int n = 9;
    int d = 1;

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

    return 0;
}

 

Recommended Post