Write an algorithm to find F(n) the number of bits set to 1, in all numbers from 1 to n for any given value of n.
F(n)
Complexity should be O(log n)
O(log n)
short and sweet!
public static int countbits(int num){ int count=0, n; while(num > 0){ n=0; while(num >= 1<<(n+1)) n++; num -= 1<