Is it possible to differentiate between 0 and -0?

前端 未结 7 1622
鱼传尺愫
鱼传尺愫 2021-01-31 07:11

I know that the integer values 0 and -0 are essentially the same. But, I am wondering if it is possible to differentiate between them.

For exam

7条回答
  •  名媛妹妹
    2021-01-31 07:20

    It depends on the machine you're targeting.

    On a machine that uses a 2's complement representation for integers there's no difference at bit-level between 0 and -0 (they have the same representation)

    If your machine used one's complement, you definitely could

    0000 0000   -> signed   0 
    1111 1111   -> signed   −0
    

    Obviously we're talking about using native support, x86 series processors have native support for the two's complement representation of signed numbers. Using other representations is definitely possible but would probably be less efficient and require more instructions.

    (As JerryCoffin also noted: even if one's complement has been considered mostly for historical reasons, signed magnitude representations are still fairly common and do have a separate representation for negative and positive zero)

提交回复
热议问题