How to check whether the bit at given position is set or unset: A Step-by-Step Guide

In this blog post, I will teach you to how to write C/C++ program to check whether the bit at given position is set or unset. After reading this detail guide post I believe you able to write an efficient program to check whether the bit at given position is set or not.

Consider the below examples,

Example-1:

Input : n = 5 //number
      : d = 2 //position want to check


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 check bit wether set or unset

Output: Bit is set

 

 

Input : n = 9 //number
      : d = 1 //position want to check


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 check bit wether set or unset

Output: Bit is unset

 

Let’s see the problem statements for this question.

Problem Statement: Given two positive number ‘n‘ and ‘d‘. You need to check whether the bit at position d from the right in the binary representation of n is set (‘1’) or unset (‘0’). That means you need check whether dth bit in ‘n’ is set or unset. 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:

Using the Bitwise And Operator (&) with a Mask you can easily check whether the bit at given position is set or unset.

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

After creating the mask, you need to perform the bitwise AND between given number ‘n’ and the mask. Now you know whether dth bit of given number ‘n’ is set or unset.

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

Now if you want to check its 2nd bit (bit-position start from the 0), 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 ANDing this mask with original number ‘n’.

Original number n: 5
                   00000101 (5 in binary)
                    &
Mask:              00000100


Result: =        Non-Zero result, so Bit is set

 

 

C Program to check whether the bit at given position is set or unset:

/*
C Program to check whether the
bit at given position is set or unset:
*/
#include <stdio.h>

//If d valid,function to check whether the bit
// at given position is set or unset
int checkDthBits(unsigned int n, int d)
{
    unsigned int MASK = 0;
    int ret = -1;

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

        // if it results to '1' then bit is set,
        // else it results to '0' bit is unset
        ret = (n & MASK)? 1 : 0;
    }

    return ret;
}


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

    int v = checkDthBits(n, d);
    switch(v)
    {
    case 0:
        printf("Bit is Unset");
        break;
    case 1:
        printf("Bit is Set");
        break;
    default:
        printf("Enter valid value of position\n");
        break;
    }

    return 0;
}

Output: Bit is Set

 

C++ Program to check whether the bit at given position is set or unset:

/*
C++ Program to check whether the
bit at given position is set or unset:
*/
#include <iostream>
#include <cassert>

// Use (void) to silence unused warnings.
#define assertm(exp, msg) assert(((void)msg, exp))



//If d valid,function to check whether the bit
// at given position is set or unset
bool checkDthBits(unsigned int n, int d)
{
    //d must be greater than 0
    assert((void("Enter valid value of position"), d>0));

    //Create MASK
    const unsigned int MASK = (1 << d);

    // if it results to '1' then bit is set,
    // else it results to '0' bit is unset
    const bool isBitSet = (n & MASK)? true : false;

    return isBitSet;
}


int main()
{
    int n = 5;
    int d = 2;
    const bool hasDthBitSet = checkDthBits(n, d);
    if(hasDthBitSet)
    {
        std::cout<<"Bit is Set"<<std::endl;
    }
    else
    {
        std::cout << "Bit is Unset"<<std::endl;
    }

    return 0;
}

Output: Bit is Set

 

Recommended Post

Leave a Reply

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