Two's Complement Representation: Difference between revisions
Line 17: | Line 17: | ||
a<sub>n-1</sub> a<sub>n-2</sub> ... a<sub>2</sub> a<sub>1</sub> a<sub>0</sub> | a<sub>n-1</sub> a<sub>n-2</sub> ... a<sub>2</sub> a<sub>1</sub> a<sub>0</sub> | ||
<sub>n-2</sub> | |||
v = -a<sub>n-1</sub>*2<sup>n-1</sup>+∑ | v = -a<sub>n-1</sub>*2<sup>n-1</sup>+∑ | ||
<sup>i=0</sup> | |||
=Practical Implications= | =Practical Implications= |
Revision as of 00:32, 6 April 2020
External
Internal
Overview
Two's complement is the most common signed integer representation scheme on computers. The scheme is widely used because a computer can use the same circuitry to perform addition, subtraction and multiplication, whereas otherwise they would have to be treated as separate operations. Also, two's complement has no representation for negative zero, and thus does it not suffer from associated difficulties.
Mathematical Foundation
A two's complement encodes both positive and negative numbers in a binary number representation. Assuming that n bits are available to represent an integral value, the weight of each bit, except the most significant one, is a power of two. The weight of the most significant bit is the negative of the corresponding power of two.
an-1 an-2 ... a2 a1 a0
n-2 v = -an-1*2n-1+∑ i=0
Practical Implications
Subtraction
used by most computers to represent signed integral values such as byte, int or long.
Positive numbers
Negative numbers
The primary motivation between this scheme is that