Bit Manipulation - Number of 1 Bits (Hamming Weight)
Consider the following Python function intended to count the number of set bits in a signed 32-bit integer:
What is the main issue with this implementation when n is negative?
def countSetBits(n: int) -> int:
count = 0
while n != 0:
count += n & 1
n >>= 1
return countWhat is the main issue with this implementation when n is negative?
