So, computers use two additions for the internal representation of integers. Ie -5 appears as ^ 5 + 1 = "1111 1011".
However, trying to print a binary representation, for example. following code:
var i int8 = -5 fmt.Printf("%b", i)
Conclusion -101 . Not quite what I expect. Is formatting different or is it using two additions?
Interestingly, converting to unsigned int results in a βcorrectβ bit pattern:
var u uint8 = uint(i) fmt.Printf("%b", u)
Conclusion 11111011 is exactly the complement of 2s to -5 .
So, it seems to me that the value inside really uses two additions, but formatting prints unsigned 5 and adds - .
Can anyone clarify this?
go
joerx
source share