Two's Complement Representation: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 18: Line 18:


                 <sub>n-2</sub>
                 <sub>n-2</sub>
  v = -a<sub>n-1</sub>*2<sup>n-1</sup> + ∑ a<sub>i</sub> * 2<sup>i</sup>
  v = -a<sub>n-1</sub>*2<sup>n-1</sup> + ∑ a<sub>i</sub> 2<sup>i</sup>
                 <sup>i=0</sup>
                 <sup>i=0</sup>



Revision as of 00:34, 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 + ∑ ai 2i
                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