Is it possible to differentiate between 0 and -0?

前端 未结 7 1450
鱼传尺愫
鱼传尺愫 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条回答
  •  旧时难觅i
    2021-01-31 07:15

    Let's begin with representing 0 in 2's complement (of course there exist many other systems and representations, here I'm referring this specific one), assuming 8-bit, zero is:

    0000 0000
    

    Now let's flip all the bits and add 1 to get the 2's complement:

    1111 1111 (flip)
    0000 0001 (add one)
    ---------
    0000 0000
    

    we got 0000 0000, and that's the representation of -0 as well.

    But note that in 1's complement, signed 0 is 0000 0000, but -0 is 1111 1111.

提交回复
热议问题