C Programming - Bitwise Operators - Discussion


Which of the following statements are correct about the program?


int main()
    unsigned int m[] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80};
    unsigned char n, i;
    scanf("%d", &n);
    for(i=0; i<=7; i++)
        if(n & m[i])
    return 0;

[A]. It will put OFF all bits that are ON in the number n
[B]. It will test whether the individual bits of n are ON or OFF
[C]. It will put ON all bits that are OFF in the number n
[D]. It will report compilation errors in the if statement.

Answer: Option B


No answer description available for this question.

Neha said: (Sep 7, 2011)  
Can anybody explain.

Subash said: (Sep 18, 2011)  
I think & operator is for both off and (on or off).

Nitesh said: (Oct 15, 2011)  
ox01= 0*16^1+1*16^0=1

Now( num & ox01 (i.e.1))==will check whthr LSB is set or not

ox02 ,ox04,ox08 will check 2ndbit,3rd bit and 4th bit
and then ox10=1*16^1+0*16^0=16
i.e 10000 hence it will check for 5th bit and so on...
like ox80= 8*16^1+0*16^0=128
i.e 1000 0000 it will check for 8th bit..

So code will check set bit upto 8 bit...

Vaageesh said: (Oct 16, 2012)  
Here i is declared as char and how it would be possible to take the integer value as i=0 and all that stuffs?

Lucky said: (Aug 12, 2014)  
Can anybody explain that is it allowed to apply logical & op to an int var and char var?

Ketul said: (Nov 5, 2014)  
Here what is value of number considered?

Manoj Kumar said: (Sep 11, 2015)  
Anyone can explain this code in a right way?

Shweta Kumari said: (Jan 13, 2017)  
Not getting this. Please explain me.

Alex said: (Mar 14, 2017)  
It might make more sense modified this way to display a binary value. Run it, modify the value of n and observe.

/* Note: GCC Compiler (32 Bit Linux Platform). */

int main()
unsigned int m[] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80};
unsigned char n, i;
n = 0x04;
for(i=7; i>0; i--)
if(n & m[i])
return 0;

Amit Tiwari said: (May 9, 2017)  
I am not getting this, Someone please explain it.

Abhishek said: (Aug 20, 2017)  
Suppose n=2.

Binary representation as, 0000 0000 0000 0010.
Now when, i=0, m[0]=0x01(in hexa) hence in binary 0000 0000 0000 0001.
n & m gives 0 (as & gives 1 only when 1 is present in same position in both numbers).
i becomes 1, m[1]=0x02 hence in binary, 0000 0000 0000 0010
n&m gives 1 hence yes is printed.
i becomes 2, m[2]=0x04 hence in binary, 0000 0000 0000 0100
n&m gives 0 again.

Just like this, it will keep checking.
Remember when m=0x10 then in binary, 0000 0000 0001 0000.
and m=0x20 then in binary, 0000 0000 0010 0000.
As you can see m value converted to binary keeps shifting the value of 1 one position to the left till m=0x80 that is, 0000 0000 1000 0000.
Hope it helps.

Rahul said: (Aug 22, 2017)  
It is clear now. Thanks @Abhishek.

Quein said: (Dec 12, 2018)  
Please explain it clearly.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.