class Solution(object):
def countBits(self, num):
"""
:type num: int
:rtype: List[int]
"""
res=[0]
for i in xrange(1,num+1):
res.append(res[i>>1]+(i&1))
return res
Python solution



@Uduse This makes me wonder if % 2 is optimized to check only the first bit. I'd assume x & y would convert both to 32bit binary form then compare each bit.