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.

User Natisha
by
6.1k points

1 Answer

4 votes
int count(int num)

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


User Roni Vered
by
6.1k points