asked 171k views
5 votes
Write a recursive method in pseudo code that returns the number of 1's in the binary representation of n. use the fact that this equal to the number of 1's in the representation of n/2, plus 1, if n is odd.

asked
User Natisha
by
7.3k points

1 Answer

4 votes
int count(int num)

if num==0 return 0;
return (n&1)+count (n/2)


answered
User Roni Vered
by
7.9k points