2016-06-02 04:12
浏览 271


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?

图片转代码服务由CSDN问答提供 功能建议

因此,计算机使用二进制补码在内部表示带符号整数。 即-5表示为^ 5 +1 =“ 1111 1011”。

但是,尝试打印二进制表示形式,例如 以下代码:

  var i int8 = -5 

输出 -101 。 不完全符合我的期望。 格式是否不同还是毕竟不使用二进制补码?

有趣的是,转换为无符号int会导致“正确”位模式: < pre> var u uint8 = uint(i) fmt.Printf(“%b”,u)

输出为 11111011 -恰好是 -5 的2s补码。

所以在我看来,该值在内部真正是使用Two的补码,但格式为 未签名的 5 并在-之前。


  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongma7796 2016-06-02 04:46

    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.

    解决 无用
    打赏 举报
  • drccfl9407 2019-04-23 19:14

    Unsafe pointers must be used to correctly represent negative numbers in binary format.

    package main
    import (
    func bInt8(n int8) string {
        return strconv.FormatUint(uint64(*(*uint8)(unsafe.Pointer(&n))), 2)
    func main() {


    解决 无用
    打赏 举报

相关推荐 更多相似问题