Two types of numbers are possible -

- Power of 2 -

They have 1 bit because`i & (i-1) == 0`

. - Not Power of 2 -

Expression`ans[i & (i-1)]`

gives the number of bit of number having one bit less than it.

```
class Solution {
public int[] countBits(int num) {
int[] ans = new int[num+1];
ans[0] = 0;
for(int i=1;i<=num;i++)
ans[i] += ans[i & (i-1)] + 1;
return ans;
}
}
```