I am a bit confused as to what (~a).toString(2)
is doing.
I'm familiar with binary and 1s complement and 2s complement.
So 5 is 000101 in 1s complement and 2s complement that number stays the same.
And -5 in 1s complement is 111010 and in 2s complement that's 111011
None of those numbers involve 110 or 100
So I don't understand how the binary results of 110 and 100 are being derived from the figure of 5 or of -5.
i also know that 1s complement is calculated by flipping bits, and I know that 2s complement is calculated by doing 1s complement and adding one. Or by a shortcut of holding all binary digits up to and including the first one you find going from the far right to the far left, and flipping the rest. And I know that for 1s complement and 2s complement of positive numbers you need one zero on the far left and more than that are ok but unnecessary. And that for negative numbers in 1s and 2s complement, you need at least one 1 on the far left, and more than that is ok but unnecessary. But none of those facts explain the result I see. (or perhaps they do but I can't see it)
I have read that tilda(~) flips bits but I don't see how this result is produced.
a=5
(~a).toString(2)
"-110"
and
a=-5
-5
(~a).toString(2)
"100"