Write a function that takes an unsigned integer (in the form of a binary string) as input and returns the number of '1' digits in its binary expression (also known as Hamming weight).
Note that the input is an integer value, not a string
Example 1:
Input: 0000000000000000000000000001011
Output: 3
Explanation: In the input binary string 00000000000000000000000000001011, a total of three bits are '1'.
Bitwise operation: every time n & (n - 1) is performed, the last occurrence of 1 in the binary of n is eliminated.
The number of operations to perform n & (n - 1) to make n become 0 is the number of 1s in the binary of n.
class Solution:def hammingWeight(self, n: int)-> int:res = 0while n>0:res+=1n &=n-1return res