How are negative numbers represented in 32-bit signed integer?

后端 未结 7 1810
时光取名叫无心
时光取名叫无心 2020-12-02 01:30

How are negative number represented in 32-bit signed integer? Is it two\'s or one\'s complement? or the last bit on the left is like a flag? For example: (-10)

相关标签:
7条回答
  • 2020-12-02 02:29

    I think the answer is 0110, preceeded by 1 repeated 28 times, therefore it looks like:

    1111 1111 1111 1111 1111 1111 1111 0110;

    Steps:

    1. bit representation for 10 is:

      0000 0000 0000 0000 0000 0000 0000 1010;

    2. 0->1 and 1->0 for all the bits:

      1111 1111 1111 1111 1111 1111 1111 0101;

    3. add 1 to the last bit, and propagate to the bit ahead, done!

      1111 1111 1111 1111 1111 1111 1111 0110;

    ===

    You can verify by adding it with 10, and you will get 0 for all bits. As mentioned above, it is 2-based and follows two's complement.

    0 讨论(0)
提交回复
热议问题