Q191. Number of 1 Bits
Last updated
Was this helpful?
Last updated
Was this helpful?
直达:
Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as the).
For example, the 32-bit integer ’11' has binary representation00000000000000000000000000001011
, so the function should return 3.
Credits: Special thanks tofor adding this problem and creating all test cases.
二进制中,n&(n-1)作用是将n的二进制表示中的最低位为1的改为0,每更改一次计数器+1,知道n=0停止。