Two's complement represenation is about negative numbers. Two's complement representation of a positive number is that number itself.
For example, Integer.bitCount(-1)
returns 32, because two's complement representation of -1
is a value with all 1
s (32 of them for int
).
But 255 is not a negative number, therefore its two's complement representation is the value 255 itself (that has 8 1
s in its representation).