LeetCode 191. Number of 1 Bits Posted on 2021-03-24 Edited on 2024-11-24 In LeetCode 1234567891011public class Solution { // you need to treat n as an unsigned value public int hammingWeight(int n) { int ans = 0; while (n != 0) { n &= n - 1; ans++; } return ans; }}