C Programming - Bitwise Operators - Discussion

Discussion Forum : Bitwise Operators - Point Out Correct Statements (Q.No. 4)
4.
Which of the following statements are correct about the program?
#include<stdio.h>

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])
            printf("yes");
    }
    return 0;
}
It will put OFF all bits that are ON in the number n
It will test whether the individual bits of n are ON or OFF
It will put ON all bits that are OFF in the number n
It will report compilation errors in the if statement.
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
13 comments Page 1 of 2.

Neha said:   1 decade ago
Can anybody explain.

Subash said:   1 decade ago
I think & operator is for both off and (on or off).

Nitesh said:   1 decade ago
ox01= 0*16^1+1*16^0=1

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

Similarly
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:   1 decade ago
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:   1 decade ago
Can anybody explain that is it allowed to apply logical & op to an int var and char var?

Ketul said:   1 decade ago
Here what is value of number considered?

Manoj kumar said:   10 years ago
Anyone can explain this code in a right way?

Shweta kumari said:   9 years ago
Not getting this. Please explain me.

Alex said:   8 years ago
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). */

#include<stdio.h>
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])
printf("1");
else
printf("0");
}
return 0;
}

Amit tiwari said:   8 years ago
I am not getting this, Someone please explain it.


Post your comments here:

Your comments will be displayed after verification.