edited by
2,339 views
1 votes
1 votes

Consider the following C++ function f():

unsigned int f(unsigned int n) {
    unsigned int b=0;
    while (n) {
        b+=n & 1;
        n>>1;
    }
return b;
}

The function f() returns the int that represents the ____P____ in the binary representation of positive integer n, where P is

  1. number of $0$’s
  2. number of bits
  3. number of consecutive $1$’s
  4. number of $1$’s
edited by

1 Answer

1 votes
1 votes
Ans $4)$

Program counting number of $1.$

It is given in question, that $n$ represent, binary representation for $n.$

Now, if we check 'while' loop, $b$ is increments it's value only  when last digit of $n$ is $1.$ and then it is right sifting the value.

So, $b$ is returning nothing but number of $1's$ in $n.$
Answer:

Related questions

2 votes
2 votes
2 answers
3
Arjun asked Jul 2, 2019
7,069 views
How many ways are there to place $8$ indistinguishable balls into four distinguishable bins?$70$$165$$^8C_4$$^8P_4$
3 votes
3 votes
2 answers
4
Arjun asked Jul 2, 2019
5,758 views
How many bit strings of length ten either start with a $1$ bit or end with two bits $00$ ?$320$$480$$640$$768$