524 views
1 1 vote
int fun(int n)
{
int table={0,1,1,2,1,2,2,3,1,2,2,3,2,3,3};
int count=0;
for(;n;n>>=4)
count=count+table[n&oxf];
return(count);
}
what will return inn the following function fun()?
a)it returns number of 0's in the number.
b)it returns number of 1's in the number
c)it returns number of 0's and 1's in the number
d)none of the above.

1 Answer

3 3 votes

Answer should be option B.

Explanation :

Let n =1000 (8 in decimal)

count = 0

initially in for loop

First iteration

n = 1000

count = count + table [n&oxf]

here anding of n and 15 number is done

1000 & 1111 = 1000

table[8] = 1

hence count = count + 1 => count =1;

Second iteration

initially n is shifted by 4 positions in right = 1000 => 0100 => 0010 => 0001 => 0000 (Here n is 0 hence loop is ended)

and value of count =1 returned.

which is number of 1's in given number (1000 => number of 1's =1)

I've performed this on another number as well it will give number of 1's everytime. :)

Position:
Show:

Related questions

–1 –1 vote
0 0 answers
712
712 views
Rajesh Panwar asked Jan 21, 2019
712 views
1 1 vote
0 0 answers
704
704 views
eyeamgj asked Jan 5, 2018
704 views
here what i thinking is every type of hazard is present so what to choose or any other view is there for question
0 0 votes
0 0 answers
658
658 views
pranab ray asked Dec 8, 2017
658 views
0 0 votes
0 0 answers
608
608 views
aaru14 asked Nov 15, 2017
608 views
if L1 and L2 are two context sensitive language and L3 is a context free thenL=L1.L2-L3 isa)regularb)context freec)context sensitived)recursive language