(Preliminary)
Follow these fast links:
Three numeration systems, binary, octal and hexadecimal, are used when dealing with the internal workings of computers. To be a knowledgeable, efficient programmer requires at least a rudimentary understanding of how they work and how they are interrelated.
The most basic computer unit of data that has an address assigned to it is the byte. It consists of eight (8) bits, each of which represents a binary digit. The numeration system used in the computer is known as Binary, or Base 2.
Our usual numeration system is known as decimal or Base 10.
It would seem natural that humans would want to use the numeration system with which they are familiar. However, the computer designers realized that computers and binary were made for each other. Electricity has two states: on and off. A binary digit has two possible values: 1 and 0.
It was obvious that 1 could represent on and 0 could be off. Consider the following four basic binary addition facts,
0 0 1 1 +0 +1 +0 +1 -- -- -- -- 0 1 1 10
which could be arranged in the following table.
|
|
||||||||||||||||||||||||||||||||
Using these addition facts, we can easily count in binary up to 10002, the equivalent of 810. The red digits indicate carrying. You will note that carrying already takes place for the addition 1 + 1, when we get the first group of 2.
1 11 1 111
0 1 10 11 100 101 110 111
+1 +1 +1 +1 +1 +1 +1 +1
-- -- -- --- --- --- --- ---
1 10 11 100 101 110 111 1000
Compare the above chart with the one for decimal below.
| Decimal Addition | |||||||||||
| + | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
| 0 | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
| 1 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 |
| 2 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |
| 3 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 |
| 4 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 |
| 5 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 |
| 6 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 |
| 7 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 |
| 8 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 |
| 9 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 |
| 10 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 |
Almost all would think that this table for decimal is extremely simplistic, because they already know it by heart. However, it has twenty-five times as many facts as the one for binary. Since any numerical value which can be expressed in one system can be expressed in the other, computer designers decided to use binary because they would need to make the computer handle so many fewer addition facts. This would obviously make their job as designers much simpler.
The only real problem with using binary was that the number of digits required to represent a value rose very quickly as the value increased. The table below shows the first eight place values for binary digits.
| Decimal-Binary Conversion | ||
| Decimal | Binary | Place Value |
| 1 | 1 | 20 |
| 2 | 10 | 21 |
| 4 | 100 | 22 |
| 8 | 1000 | 23 |
| 16 | 10000 | 24 |
| 32 | 100000 | 25 |
| 64 | 1000000 | 26 |
| 128 | 10000000 | 27 |
All values up to 25510 can be represented by combinations of 1s and 0s in binary numbers using up to a total of eight digits. For example, 1510 is equivalent to 11112 based on the sum of the place values of the 1s digits.
| Decimal | Binary | Sum of Place Values |
| 15 = | 1111 = | 23 + 22 + 21 + 20 |
Since numbers represented in binary quickly have a great number of digits (25510 = 111111112), two other systems are used to make things easier for humans, Octal (Base 8) and Hexadecimal (Base 16). The following table illustrates counting using the whole number values from 010 to 2010 in all four bases.
| Counting in Different Bases | |||
| Decimal (10) | Binary (2) | Octal (8) | Hex (16) |
| 0 | 0 | 0 | 0 |
| 1 | 1 | 1 | 1 |
| 2 | 10 | 2 | 2 |
| 3 | 11 | 3 | 3 |
| 4 | 100 | 4 | 4 |
| 5 | 101 | 5 | 5 |
| 6 | 110 | 6 | 6 |
| 7 | 111 | 7 | 7 |
| 8 | 1000 | 10 | 8 |
| 9 | 1001 | 11 | 9 |
| 10 | 1010 | 12 | A |
| 11 | 1011 | 13 | B |
| 12 | 1100 | 14 | C |
| 13 | 1101 | 15 | D |
| 14 | 1110 | 16 | E |
| 15 | 1111 | 17 | F |
| 16 | 10000 | 20 | 10 |
| 17 | 10001 | 21 | 11 |
| 18 | 10010 | 22 | 12 |
| 19 | 10011 | 23 | 13 |
| 20 | 10100 | 24 | 14 |
Notice that the number of the base indicates the total number of different symbols used in that system.
Octal was used originally for two reasons:
| 101 1102 | = | 5 68 |
| Binary-Triplet/Octal Conversion | ||||||||
| Binary | 000 | 001 | 010 | 011 | 100 | 101 | 110 | 111 |
| Octal | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
The mathematical relationship between the octal and binary systems can easily be demonstrated using the concept of place value.
| 1011102 | = | Digit-by-digit conversion to base 10 1 x 25 + 0 x 24 + 1 x 23 + 1 x 22 + 1 x 21 + 0 x 20 |
| = | Extract common factors 23(1 x 22 + 0 x 21 + 1 x 20) + 20(1 x 22 + 1 x 21 + 0 x 20) | |
| = | Simplify 8(4 + 0 + 1) + 1(4 + 2 + 0) | |
| = | Simplify 8(5) + 1(6) | |
| = | Convert to powers of 8 81(5) + 80(6) | |
| = | Commutative Property 5 x 81 + 6 x 80 | |
| 1011102 | = | Digit-by-digit conversion to base 8 568 Q.E.D. |
Fortunately, for us humans, we do not need to use this latter method. All we have to do is use the table above, which can be easily memorized (or generated as needed).
Many programmers became very proficient in their use of octal. Not only could they convert between binary and octal with ease, they could also do arithmetic in octal. A quick look at this table of octal addition facts shows that they did not have to learn much that was different from the decimal addition facts they already knew.
Two things conspired against the use of octal as the preferred shorthand for binary:
The introduction of the 8-bit byte ensured the use of hexadecimal, Base 16, in computing. Its nickname is hex. The byte could be treated as being composed of two 4-bit units, which are called nibbles -- something intermediate between a little bit and a big byte.
Life is filled with trade-offs. Making one thing easier or better often requires learning more. Consider these facts.
To directly address 128 Meg of RAM requires an address 28 bits long. This would result in an octal representation of 10 digits and in hexadecimal of 7. CPUs can now have 32 bits in a register. This would take 11 octal digits or just 8 in hexadecimal.
Binary still remains important. A single bit can serve as a boolean variable. Finding the particular bit which is being used for a boolean out of 32 bits is not easy and the programmer is prone to making mistakes. It is much easier to find the value of a specific bit by scanning the hex value for the nibble which contains it and then do the hex-digit to binary conversion using the chart below.
To convert from binary to hex, break up the binary number into groupings of four binary digits, i.e., nibbles. Then using the table below, find the corresponding hex digit for each nibble, e.g.,
| 1111 1101 0000 01102 | = | F D 0 616 |
| Binary-Quartet/Hexadecimal Conversion | ||||||||||||||||
| Binary | 0000 | 0001 | 0010 | 0011 | 0100 | 0101 | 0110 | 0111 | 1000 | 1001 | 1010 | 1011 | 1100 | 1101 | 1110 | 1111 |
| Hexadecimal | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | A | B | C | D | E | F |
| Decimal | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 |
There is a major downside to using hexadecimal -- there are a lot more addition facts to learn. The use of symbols with unfamiliar values (A, B, C, D, E, F) only compounds the problem. Thank goodness for hand-held calculators!
| Hexadecimal Addition | |||||||||||||||||
| + | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | A | B | C | D | E | F | 10 |
| 0 | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | A | B | C | D | E | F | 10 |
| 1 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | A | B | C | D | E | F | 10 | 11 |
| 2 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | A | B | C | D | E | F | 10 | 11 | 12 |
| 3 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | A | B | C | D | E | F | 10 | 11 | 12 | 13 |
| 4 | 4 | 5 | 6 | 7 | 8 | 9 | A | B | C | D | E | F | 10 | 11 | 12 | 13 | 14 |
| 5 | 5 | 6 | 7 | 8 | 9 | A | B | C | D | E | F | 10 | 11 | 12 | 13 | 14 | 15 |
| 6 | 6 | 7 | 8 | 9 | A | B | C | D | E | F | 10 | 11 | 12 | 13 | 14 | 15 | 16 |
| 7 | 7 | 8 | 9 | A | B | C | D | E | F | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 |
| 8 | 8 | 9 | A | B | C | D | E | F | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 |
| 9 | 9 | A | B | C | D | E | F | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 |
| A | A | B | C | D | E | F | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 1A |
| B | B | C | D | E | F | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 1A | 1B |
| C | C | D | E | F | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 1A | 1B | 1C |
| D | D | E | F | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 1A | 1B | 1C | 1D |
| E | E | F | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 1A | 1B | 1C | 1D | 1E |
| F | F | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 1A | 1B | 1C | 1D | 1E | 1F |
| 10 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 1A | 1B | 1C | 1D | 1E | 1F | 20 |
| Hexadecimal Multiplication | |||||||||||||||||
| x | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | A | B | C | D | E | F | 10 |
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 1 | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | A | B | C | D | E | F | 10 |
| 2 | 0 | 2 | 4 | 6 | 8 | A | C | E | 10 | 12 | 14 | 16 | 18 | 1A | 1C | 1E | 20 |
| 3 | 0 | 3 | 6 | 9 | C | F | 12 | 15 | 18 | 1B | 1E | 21 | 24 | 27 | 2A | 2D | 30 |
| 4 | 0 | 4 | 8 | C | 10 | 14 | 18 | 1C | 20 | 24 | 28 | 2C | 30 | 34 | 38 | 3C | 40 |
| 5 | 0 | 5 | A | F | 14 | 19 | 1E | 23 | 28 | 2D | 32 | 37 | 3C | 41 | 46 | 4B | 50 |
| 6 | 0 | 6 | C | 12 | 18 | 1E | 24 | 2A | 30 | 36 | 3C | 42 | 48 | 4E | 54 | 5A | 60 |
| 7 | 0 | 7 | E | 15 | 1C | 23 | 2A | 31 | 38 | 3F | 46 | 4D | 54 | 5B | 62 | 69 | 70 |
| 8 | 0 | 8 | 10 | 18 | 20 | 28 | 30 | 38 | 40 | 48 | 50 | 58 | 60 | 68 | 70 | 78 | 80 |
| 9 | 0 | 9 | 12 | 1B | 24 | 2D | 36 | 3F | 48 | 51 | 5A | 63 | 6C | 75 | 7E | 87 | 90 |
| A | 0 | A | 14 | 1E | 28 | 32 | 3C | 46 | 50 | 5A | 64 | 6E | 78 | 82 | 8C | 96 | A0 |
| B | 0 | B | 16 | 21 | 2C | 37 | 42 | 4D | 58 | 63 | 6E | 79 | 84 | 8F | 9A | A5 | B0 |
| C | 0 | C | 18 | 24 | 30 | 3C | 48 | 54 | 60 | 6C | 78 | 84 | 90 | 9C | A8 | B4 | C0 |
| D | 0 | D | 1A | 27 | 34 | 41 | 4E | 5B | 68 | 75 | 82 | 8F | 9C | A9 | B6 | C3 | D0 |
| E | 0 | E | 1C | 2A | 38 | 46 | 54 | 62 | 70 | 7E | 8C | 9A | A8 | B6 | C4 | D2 | E0 |
| F | 0 | F | 1E | 2D | 3C | 4B | 5A | 69 | 78 | 87 | 96 | A5 | B4 | C3 | D2 | E1 | F0 |
| 10 | 0 | 10 | 20 | 30 | 40 | 50 | 60 | 70 | 80 | 90 | A0 | B0 | C0 | D0 | E0 | F0 | 100 |
The eight bits in a byte can be used to represent 256 different values, from 010 to 25510 (in decimal) or from 016 to FF16 (in hexadecimal). Hex still has a role to play at the byte level. It is worth knowing hex and its relationships to binary and decimal.