Resources | Subject Notes | Computer Science
This section explains how to represent both positive and negative integers using the two's complement number system with 8 bits. This is a common method used in computers to represent signed integers.
Recall that binary is a base-2 number system using only 0 and 1. Each bit represents a power of 2, from right to left: $2^0$, $2^1$, $2^2$, ..., $2^7$ (for 8 bits).
For positive integers, the two's complement representation is the same as the standard binary representation.
For example:
To represent negative integers, we use the two's complement method. This involves the following steps:
Example: Representing -5 in 8-bit two's complement
Therefore, the 8-bit two's complement representation of -5 is 11111011.
The following table shows the two's complement representation of integers from -128 to +127.
Decimal | Binary |
---|---|
0 | 00000000 |
1 | 00000001 |
2 | 00000010 |
3 | 00000011 |
4 | 00000100 |
5 | 00000101 |
127 | 01111111 |
128 | 10000000 |
129 | 10000001 |
-1 | 11111111 |
-2 | 11111110 |
-127 | 10000000 |
Two's complement simplifies arithmetic operations in computers because addition and subtraction can be performed using the same circuitry, regardless of the sign of the numbers.