What is 2's Complement Number? [closed]

做~自己de王妃 提交于 2019-11-26 23:41:29

问题


What is 2's Complement Number?
Why do we take 1's Complement and add 1 to it? Why don't we subtract 1 after taking 1's Complement?
Why do computers use 2's Complement?


回答1:


What is 2's Complement Number?

Complementary number system is used to represent negative numbers. So, 2's Complement number system is used to represent negative numbers.

UPDATE

Q:  What “2’s Complement System” says?

A: The negative equivalent of binary number is its 2’s complement. (1’s Complement + 1)

Note: 1 extra bit is required to represent the sign of a number. MSB (Most Significant Bit) is used as sign bit. If MSB is 0, then the number is positive. If MSB is 1, then the number is negative.

1’s Complement  Value   2’s Complement
    011         +3          011
    010         +2          010
    001         +1          001
    000         +0          000
    111         -0          000
    110         -1          111
    101         -2          110
    100         -3          101
                -4          100

How '100' (3 bits) is -4?

MSB is used as sign, if 1, its negative, if 0 it is positive.

-1 * 2^2 + 0*2^1 + 0*2^0 = -4 + 0 + 0 = -4

Similarly 101 (3 bits) is -3

-1 * 2^2 + 0*2^1 + 1*2^0 = -4 + 0 + 1 = -3

Observations:

•   In 1’s complement, using 3 bits, we represented 2^3 = 8 numbers i.e from -3 to +3.
•   In 1’s complement, -0 and +0 are having 2 representation. (+0 is ‘000’ and -0 is ‘111’).
    But mathematically +0 and -0 are same.
•   In 2’s complement, using 3 bits, we represented only 2^3 = 8 numbers i.e from -4 to +3.
•   In 2’s complement, -0 and +0 are having same representation.
•   Since +0 and -0 in 2’s complement is having same representation, 
    we are left out with one more combination which is ‘100’ = -4.

Why do we take 1's Complement and add 1 to it? Why don't we subtract 1 after taking 1's Complement?

Refer "Why Inversion and Adding One Works" topic in the below link. If I start explaining, this post will grow big. http://www.cs.cornell.edu/~tomf/notes/cps104/twoscomp.html

Why computer uses 2' Complement?

  • Cos' of less hardware. If the computer is using 2' Complement means, it does subtraction using addition circuit. So, less hardware!!!
  • As seen in the above example, +0 and -0 have same representation. (1's complement and sign magnitude representation have 2 different representation for +0 and -0).
  • (Not an important) You will be able to represent one extra number using 2's complement. (in the above example its -4 which is '100' in binary using 3 bits).



回答2:


From Wikipedia Two's Complement :

The two's-complement system has the advantage that the fundamental arithmetic operations of addition, subtraction, and multiplication are identical to those for unsigned binary numbers (as long as the inputs are represented in the same number of bits and any overflow beyond those bits is discarded from the result). This property makes the system both simpler to implement and capable of easily handling higher precision arithmetic. Also, zero has only a single representation, obviating the subtleties associated with negative zero, which exists in one's-complement systems.




回答3:


The reason why we use two's complement rather than one's complement is to make arithmetic as simple as possible.

Consider that in one's complement, 1111 1111 and 0000 0000 are the same number - by subtracting one, we have ended up with...the same number. This is too big of a pain to have to think about - so instead we use two's complement, where 1111 1111 is -1 - by subtracting one, we successfully subtract one. Hooray!

(A secondary advantage is that we can represent one more unique number in two's complement than in one's complement — in two's complement -128 to +127 instead of one's complement -127 to +127.)



来源:https://stackoverflow.com/questions/16728492/what-is-2s-complement-number

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!