OOPortal OOPortal


Data Types  «Prev  Next»
Lesson 6Two's complement representation
ObjectiveRepresent negative numbers in two's complement representation.

Two's Complement Representation

So far we have looked at how to represent non-negative numbers in binary form. To be more precise, we have really only looked at how to represent non-negative integers in binary form. To represent both positive and negative integers in binary form we make use of the leftmost bit as a sign bit. If the leftmost bit is 0, then the number is either zero or a positive integer. If the leftmost bit is 1, then the number is negative.
The use of a sign bit does not change the way in which non-negative integers are represented. For example, the decimal number 26 would still be represented in 8-bit binary form as 00011010. To represent negative integers in binary form you might suspect that we simply take the binary form for the corresponding positive number and change the sign bit from 0 to 1. It's not quite that straightforward.
Negative integers are represented in what is called two's complement form.
To obtain the two's complement representation for a negative integer, we begin with the binary representation of the corresponding positive integer. Then we reverse each digit (the 1s become 0s and the 0s become 1s) and add 1 to the result.
As an example, let's determine the 8-bit binary form for the negative integer, -3. We start with the 8-bit binary form for 3, which is 00000011. Next we reverse each digit to produce 11111100. Finally, we add 1 as shown below to obtain 11111101.
Determining the decimal number represented by a two's complement binary number involves the same steps. For example, suppose you have the 8-bit binary number 10110111. Because the leftmost bit is 1 you know this is a negative number, but which one? To find out you would reverse each digit to produce 01001000 and then add 1 to obtain 01001001. This is the 8-bit binary representation of the decimal number 73--thus our original binary number is the two's complement representation of the decimal number -73.

Structured Computer Organization
Two's complement: Binary representation for signed integers. Note that with 8 bits you can still represent 28 or 256 numbers, but when using the 8 bits to represent signed integers you can represent the integers -128 to 127 rather than the integers 0 to 255. With n bits you can represent the signed integers -2n-1 to 2n-1 - 1.
Here is a table showing the 8-bit two's complement binary numbers and their decimal equivalents.

Binary Decimal
0000000
0000001
0000010
.
.
.
01111111
10000000
10000001
10000010
.
.
.
11111111
   0
   1
   2
   .
   .
   .
 127
-128
-127
-126
   .
   .
   .
  -1

You know know how signed integers are stored in a computer. Next we will examine how real numbers are stored.

Twos Complement Tepresentation - Quiz

Click the Quiz link below to test your understanding of two's complement representation.
Twos Complement Tepresentation - Quiz