Bitwise Operators

Q1:
Which of the following statements are correct about the program?
#include<stdio.h>

int main()
{
    unsigned int num;
    int c=0;
    scanf('%u', &num);
    for(;num;num>>=1)
    {
        if(num & 1)
            c++;
    }
    printf('%d', c);
    return 0;
}

A It counts the number of bits that are ON (1) in the number num.

B It counts the number of bits that are OFF (0) in the number num.

C It sets all bits in the number num to 1

D Error

ANS:A - It counts the number of bits that are ON (1) in the number num.

If we give input 4, it will print 1.
Binary-4 == 00000000 00000100 ; Total number of bits = 1.

If we give input 3, it will print 2.
Binary-3 == 00000000 00000011 ; Total number of bits = 2.

If we give input 511, it will print 9.
Binary-511 == 00000001 11111111 ; Total number of bits = 9.



img not found
img

For help Students Orientation
Mcqs Questions

One stop destination for examination, preparation, recruitment, and more. Specially designed online test to solve all your preparation worries. Go wherever you want to and practice whenever you want, using the online test platform.