Bit check using function check(var,pos) ((var)&(1<<pos))

Joined
Aug 2, 2012
Messages
7
Reaction score
0
I have written one small program to check whether particular bit is set or not.


#define check(var,pos) ((var)&(1<<pos))
void main()
{

int var=30;// in binary 0011 0000 that is its 4th and 5th bits are set;
int bit;
printf("enter position where we want to check whether bit is set or not ");
scanf("%d",&pos);
bit=check(var,pos);
printf("%d",bit);
getch();

}


output: when entered position
0th(1<<0=0000 0001), bit value is 0
1st(1<<1=0000 0010), bit value is 2
2nd(1<<2=0000 0100), bit value is 4
3rd1<<3=0000 1000), bit value is 8
4th (1<<4 =0001 0000) , bit value is 16
and when entered position is 5th(1<<5=0010 0000), bit value is 0.
please let me know why it is so?what check(var,pos) returns?
 
Last edited:

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top