Q39 How to check a particular bit is set or not? Write a program to count number of bits set in an integer.

There are many methods to check a particular bit is set or not.

Method 1:

Suppose I want to check in a given number 3rd bit is set or not.

int  num = 10;

int temp ;

temp = num;

temp = temp >> 3

if (temp & 1)

{

            3rd bit is set;

}

else

{

            3rd bit is not set;

}

Method 2:

int  num = 10;

if (num & 0x4)

{

            3rd bit is set;

}

else

{

            3rd bit is not set;

}

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.