191. Number of 1 Bits

202401192310
tags: #bit-manipulation

var hammingWeight = function(n) {
	let res = 0;
	while (n) {
		const bit = n & 1;
		if (bit === 1) {
			res += 1;
		}
		n >>>= 1;
	}
	return res;
};

Reference

https://leetcode.com/problems/number-of-1-bits/