search
Log In
Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
1 vote
552 views

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
in Object Oriented Programming
edited by
552 views

1 Answer

1 vote
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.$

Related questions

4 votes
3 answers
1
949 views
Consider the following C-code fragment running on a $32$-bit $X86$ machine: typedef struct { union { unsigned char a; unsigned short b; } U; unsigned char c; }S; S B[10]; S*p=&B[4]; S*q=&B[5]; p → U.b=0x1234; /* structure S takes 32-bits */ If M is the value of $q-p$ and $N$ is the value of $((\text{int )&} ( p \rightarrow c)) – ((\text{int})p)$, then $(M,N)$ is $(1,1)$ $(3,2)$ $(1,2)$ $(4,4)$
asked Jul 2, 2019 in Programming Arjun 949 views
8 votes
3 answers
2
1.9k views
Consider the poset $( \{3,5,9,15,24,45 \}, \mid).$ Which of the following is correct for the given poset ? There exist a greatest element and a least element There exist a greatest element but not a least element There exist a least element but not a greatest element There does not exist a greatest element and a least element
asked Jul 2, 2019 in Set Theory & Algebra Arjun 1.9k views
2 votes
2 answers
3
1.4k views
How many ways are there to place $8$ indistinguishable balls into four distinguishable bins? $70$ $165$ $^8C_4$ $^8P_4$
asked Jul 2, 2019 in Combinatory Arjun 1.4k views
2 votes
2 answers
4
822 views
How many bit strings of length ten either start with a $1$ bit or end with two bits $00$ ? $320$ $480$ $640$ $768$
asked Jul 2, 2019 in Combinatory Arjun 822 views
...