--

Two’s complement and negative numbers

In this system of representation, positive numbers are expressed as in Sign Magnitude or in Pure Binary. However, to write the negative numbers the Base Complement is used. Formally, the Complement to the Base of a positive integer N of base b, is expressed as follows:

Cb(N) = bn — N

where n is the number of figures used in the representation of the number. Therefore, in binary coding, the Complement a 2 (C2) of a positive integer (N) can be expressed as:

C2(N) = 2n — N = NC2

The representation range in Complement a 2 is:

Examples

Example 1: In Complement to 2, for n = 16, the representation range is:

Example 2: In Complement a 2, for n = 16, the positive number 950310 is represented as in Complement a 1, Sign Magnitude or Pure Binary:

950310 = 0010010100011111C2 = 0010010100011111C1 = 0010010100011111SM = 0010010100011111BP

Example 3: In Complement a 2, for n = 16, the number -950310 is written by calculating the Complement to the Base of the number 950310 represented in base 2, that is, of the number 100101000111112:

C2(N) = C2(10010100011111) =

= 2n — N =

= 216–10010100011111 =

= 10000000000000000–10010100011111 =

= 1101101011100000C2

In consecuense,

  • 950310 = 1101101011100001C2

Note that the Complement to 2 of a positive integer (N) written in binary is the result of changing all the bits of value (zeros by ones and ones by zeros), thus obtaining the complement to 1 of the number and, later, add 1

C2(N) = C1(N) + 1 = NC1 + 1 = NC2

In this way, in just two steps you can easily calculate the Complement to 2. For example, given the number:

to calculate the Complement to 2, in a first step the Complement to 1 of said number is calculated:

and, in a second step, 1 is added to the number obtained in Complement a 1

The result is as expected:

On the other hand, if you want to calculate the value in base 10 of a positive integer (N) written Complement to 2, you can use the same formula as in Sign Magnitude or in Pure Binary.

A number (N) represented in Complement a 2 is positive if the most significant bit is zero. Otherwise, the number will be negative, and to calculate its value in base 10, the formula must be used:

Once you understand how it works, the two’s complement is actually the easiest way for either a human or a computer to deal with negative numbers in binary.

--

--