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?
#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: