Your browser does not seem to support JavaScript. As a result, your viewing experience will be diminished, and you have been placed in read-only mode.

Please download a browser that supports JavaScript, or enable it if it's disabled (i.e. NoScript).

class Solution { public: int hammingWeight(uint32_t n) { int ans=0; while (n) ans += n&1, n=n>>1; return ans; } };

What if n is equal to 10000000000000000000000000000000 (binary)? There's a way to execute the while only once :)

class Solution { public: int hammingWeight(uint32_t n) {

int count=0; if (n==0) return 0; while (n&(n-1)) { count++; n=n&(n-1); } count++; return count; }

};

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.