C Language | Computer System Development | Data Representation
Computers represent information with two electrical states, on and off. This article explains binary numbers and complements used to represent negative values.
Radix
People normally use decimal notation with the ten digits from 0 to 9. Computers instead distinguish voltage states and represent them as 0 and 1. A number written with these two symbols is a binary number. In binary notation, each position has a value that is a power of two.
Table 1 - Decimal and binary numbers
| Decimal | Binary |
|---|---|
| 0 | 0 |
| 1 | 1 |
| 2 | 10 |
| 3 | 11 |
| 4 | 100 |
| 5 | 101 |
| 6 | 110 |
| 7 | 111 |
| 8 | 1000 |
| 9 | 1001 |
Binary notation becomes difficult to read as values grow. For this reason, hexadecimal notation is commonly used when people work directly with digital data. Hexadecimal notation uses the letters A through F for values above 9.
Table 2 - Common radices
| Decimal | Binary | Base 4 | Octal | Hexadecimal |
|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 |
| 1 | 1 | 1 | 1 | 1 |
| 2 | 10 | 2 | 2 | 2 |
| 3 | 11 | 3 | 3 | 3 |
| 4 | 100 | 10 | 4 | 4 |
| 5 | 101 | 11 | 5 | 5 |
| 6 | 110 | 12 | 6 | 6 |
| 7 | 111 | 13 | 7 | 7 |
| 8 | 1000 | 100 | 10 | 8 |
| 9 | 1001 | 101 | 11 | 9 |
| 10 | 1010 | 102 | 12 | A |
| 11 | 1011 | 103 | 13 | B |
| 12 | 1100 | 110 | 14 | C |
| 13 | 1101 | 111 | 15 | D |
| 14 | 1110 | 112 | 16 | E |
| 15 | 1111 | 113 | 17 | F |
Digital data is fundamentally binary, but programming languages and binary editors often display it in hexadecimal.
Negative Values and Complements
Binary digits do not include a sign. Computers commonly represent negative integers with two’s complement notation.
The one’s complement of a binary value is obtained by reversing every bit. The two’s complement is obtained by adding 1 to the one’s complement.
one's complement of 1100 1010 = 0011 0101
two's complement of 0110 1101 = 1001 0011
For a signed bit sequence, the most significant bit indicates the sign: 0 for a non-negative value and 1 for a negative value. For example, the 8-bit sequence 1111 1100 represents -4, because its two’s complement is 0000 0100.
Two’s complement representation also allows subtraction to be performed as addition. It is a fundamental concept in computer systems and is important when learning C.