Edit: Problem solved, advert your eyes from the below code though, haha, I had some things conceptually wrong from the beginning.
I've been trying for longer than I care to admit to count the non-zero bits in a wide variety of 32-bit integers, but always seem a bit off.
So for example, the correct amount of non-zero bits for the numbers:
13676 9190872 136669 -17621 -1631 -183 15570 0 495 468656377 -1340216 -91
Would be:
8 12 10 26 25 27 8 0 8 18
However, I always end up a few digits off on at least a few (especially the negative numbers) no matter what I try.
Currently I'm using:
def bitCount():
numbers = input().split()
answer = []
for num in numbers:
data = bin(((1 << 32) + int(num)) & -5)
bitCount = 0
for char in data[3:]:
if char == '1':
bitCount += 1
answer.append(str(bitCount))
print(' '.join(answer))
bitCount()
Which for the above set of numbers generates: 7 12 9 25 24 26 8 0 7 18 19 26
What am I doing wrong? How would I go about solving this problem? I can't imagine it is too complex, but after hours of searching, reading, and experimenting I feel like I must just be missing something obvious. Any help would be greatly appreciated.