Thursday, July 31, 2014

How to check whether particular bit is on or off in C language?



C language
C language
To check for particular bit , whether it is on or off we use "&" (AND) operator. Go through simple example given below,

Let consider 8-bit binary number,

Binary(8 bit) = 0000 1101
suppose we want check 3rd bit is on or off(on=1 & off=0), just like this,

0000 1100 (Decimal=13)
&
0000 0100 (Decimal=4,we are checking 3rd bit so it is 1)
----------
0000 0100 (Decimal=4)
----------
If we got some value then checking bit is on,otherwise off.
In C language,

if(13&4)
print("The third bit is on");
else
print("The third bit is off");

You may also like this post : Dangling Pointer