I'm solving a task using Python.
I need to count the number of non-zero bits in a given value. I don't have any problems with positive numbers as I use the function bin() and count the number of '1', but can't understand what's wrong with negative. In example, -1 is equal to 11111111111111111111111111111111, but when I use the function bin() it gives '-0b1'. Please, tell me how can I convert it right.
Just need to add 2**32 (or 1 << 32) to the negative value.
bin(-1+(1<<32))
'0b11111111111111111111111111111111'
or use following command for all the values:
a = bin(val if val>0 else val+(1<<32))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With