C Programming - Bitwise Operators - Discussion

Discussion Forum : Bitwise Operators - Point Out Correct Statements (Q.No. 2)
2.
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;
}
It counts the number of bits that are ON (1) in the number num.
It counts the number of bits that are OFF (0) in the number num.
It sets all bits in the number num to 1
Error
Answer: Option
Explanation:

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.

Discussion:
12 comments Page 1 of 2.

Sibysagar said:   2 years ago
@Aami.

As we num>>=1 right shift the value of num, when it reaches binary value 0000000000000000 it gets out of for loop.

If we give num=4;
then the loop runs for 3 three times and the num value will be;

1st time entering loop num will be 4 (0000000000000100) after the right shift it will be(0000000000000010).

2nd time entering loop num will be 2 (0000000000000010) after the right shift it will be(0000000000000001).

3rd time entering loop num will be 1 (0000000000000001) after the right shift it will be (0000000000000000).

4th time entering loop num will be 0 (0000000000000000) so code will exit for a loop.

Pavan said:   4 years ago
T+=i&1 is same as T=T+(i&1) i.e,adding result (i&1) to initial value of T and again storing it in T.

Suheel said:   4 years ago
What "T+=i&1;" means?

Mihir said:   8 years ago
Thanks @Kadarkarai Selvam.

Aami said:   8 years ago
No termination condition for FOR LOOP? Will it runs infinitely.

Lohith said:   9 years ago
Num &1 means doing AND operation for num and 1 for example.

Num = 0010(2).

1 = 0001.
-----------.
0000 (Here c will no increment and goes to for loop).

For next loop right bit shift was implemented.

Num = 0001(2).

1 = 0001.
-----------.
0001 (here c will be increment and goes to for loop).

This process was continued up to num times.

if (0)
{
Terminated the if condition and goes to for loop.
}
if (1)
{
Execute the if condition.
}

Ash said:   1 decade ago
What is the meaning of if(num&1)?

Kadarkarai Selvam said:   1 decade ago
Let us take num=20
Here num>>=1 means that num=num>>1
In for loop,
At num =20 ie 0001 0100
Now num&1 gives 000000 so it wont increment c

Then at next loop,
num= num>>1 ie 0000 1010
Now num&1 gives 0000 0000 so it wont increment c

Then at next loop,
num= num>>1 ie 0000 0101
Now num&1 gives 0000 0001 so it increment c

Like that it goes and c increments for 2 times in this case..
Thus it gives the number of ON bits in num..

Varun said:   1 decade ago
I think it is short hand operator such that num=num>>1 ?

Spa said:   1 decade ago
@vijay yes i too have d same doubt.
what is mean by num>>=1 ?


Post your comments here:

Your comments will be displayed after verification.