Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two's complement and fmt.Printf

Tags:

go

So computers use Two's complement to internally represent signed integers. I.e., -5 is represented as ^5 + 1 = "1111 1011".

However, trying to print the binary representation, e.g. the following code:

var i int8 = -5
fmt.Printf("%b", i)

Outputs -101. Not quite what I'd expect. Is the formatting different or is it not using Two's complement after all?

Interestingly, converting to an unsigned int results in the "correct" bit pattern:

var u uint8 = uint(i)
fmt.Printf("%b", u)

Output is 11111011 - exactly the 2s complement of -5.

So it seems to me the value is internally the really using Two's complement, but the formatting is printing the unsigned 5 and prepending a -.

Can somebody clarify this?

like image 436
joerx Avatar asked Jun 02 '16 04:06

joerx


1 Answers

I believe the answer lies in how the fmt module formats binary numbers, rather than the internal format.

If you take a look at fmt.integer, one of the very first actions that the function does is to convert the negative signed integer to a positive one:

   165      negative := signedness == signed && a < 0
   166      if negative {
   167          a = -a
   168      }

There's then logic to append - in front of the string that's output here.

IOW -101 really is - appended to 5 in binary.

Note: fmt.integer is called from pp.fmtInt64 in print.go, itself called from pp.printArg in the same function.

like image 138
Frederik Deweerdt Avatar answered Oct 27 '22 07:10

Frederik Deweerdt