Learn › Topic 6
Number systems: binary, octal, hexadecimal and codes
7 min read · bitGateLab Team
A digital circuit understands only zeros and ones, but you need to represent quantities, signs, the digits of a display and the positions of an encoder. Here you will learn how positional systems work, how to convert between decimal, binary, octal and hexadecimal, how to represent negative numbers in 2's complement and how to use two codes that are very common in electronics: BCD and Gray.
Positional systems
In a positional system with base b, each digit is multiplied by a power of b according to its position, counting from 0 at the right. Decimal 347 is 3·102 + 4·101 + 7·100. The same happens with the other bases:
- Binary (base 2): digits 0 and 1, called bits. Weights 1, 2, 4, 8, 16, 32, 64, 128...
- Octal (base 8): digits 0 to 7. Each octal digit is equivalent to 3 bits.
- Hexadecimal (base 16): digits 0 to 9 and A to F (A = 10, B = 11, C = 12, D = 13, E = 14, F = 15). Each hexadecimal digit is equivalent to 4 bits.
To avoid confusion, the base is indicated with a subscript (10102, 178, 3F16) or with a prefix such as 0b or 0x.
Converting between bases
From any base to decimal
Multiply each digit by its weight and add. For example, 2F316 = 2·256 + 15·16 + 3·1 = 512 + 240 + 3 = 755.
From decimal to binary
Divide by 2 repeatedly and write down the remainders; the binary number is read from the bottom up (the last remainder is the most significant bit). Another way is to subtract the largest power of 2 that fits, over and over.
Between binary, octal and hexadecimal
Since 8 = 23 and 16 = 24, there is no need to go through decimal: group the bits in sets of 3 (octal) or in sets of 4 (hexadecimal) starting from the right, and pad with zeros on the left if necessary.
Example 1: converting 156 to binary, octal and hexadecimal.
- Successive divisions: 156/2 = 78 r 0; 78/2 = 39 r 0; 39/2 = 19 r 1; 19/2 = 9 r 1; 9/2 = 4 r 1; 4/2 = 2 r 0; 2/2 = 1 r 0; 1/2 = 0 r 1.
- Reading the remainders from the bottom up: 156 = 100111002. Check: 128 + 16 + 8 + 4 = 156.
- Octal: 10 011 100 → 010 011 100 → 2 3 4. Result 2348 (2·64 + 3·8 + 4 = 156).
- Hexadecimal: 1001 1100 → 9 C. Result 9C16 (9·16 + 12 = 156).
Negative numbers: 2's complement
An n-bit register has no place for the "minus" sign, so a convention is used. The one used by practically every processor is 2's complement: the most significant bit has a negative weight, −2n−1, and the others keep their positive weight. With 8 bits the range runs from −128 to +127; with 4 bits, from −8 to +7.
To obtain the negative of a number:
- Write the positive number with all its bits.
- Invert every bit (this is the 1's complement).
- Add 1.
The advantage is huge: addition and subtraction are done with the same adder circuit, with no special logic for the sign. There is a single zero (00000000), and the most significant bit tells you directly whether the number is negative.
Example 2: representing −45 in 8 bits and computing 25 + (−12).
- 45 = 00101101. Inverting: 11010010. Adding 1: 11010011 = −45.
- Check with the weights: −128 + 64 + 16 + 2 + 1 = −45.
- Now 25 + (−12). 25 = 00011001; 12 = 00001100, inverted 11110011, plus 1: 11110100 = −12.
- Sum: 00011001 + 11110100 = 1 00001101. The carry out of bit 8 is discarded.
- Result: 00001101 = 13. Correct.
BCD code
In BCD (binary-coded decimal, 8421 version) each decimal digit is represented separately with 4 bits. So 395 is written 0011 1001 0101. This is not the same as converting 395 to binary (1100010112): BCD uses more bits, but each group goes straight to a 7-segment display decoder, with no divisions. The codes 1010 to 1111 are not valid in BCD; in a design, those combinations are usually treated as don't-care conditions on Karnaugh maps.
Gray code
In Gray code, only one bit changes between one value and the next. In ordinary binary, going from 7 (0111) to 8 (1000) changes all four bits at once; if a sensor reads right in the middle of the transition, it may briefly see any intermediate value. With Gray code that error cannot happen, which is why it is used in position encoders, in counters that cross between different clocks and in the headings of Karnaugh maps.
- Binary to Gray: the most significant bit is copied; each following bit is the XOR of the corresponding binary bit with the binary bit to its left. As a formula, g = b ⊕ (b shifted one place to the right).
- Gray to binary: the most significant bit is copied; each following binary bit is the XOR of the binary bit just obtained with the Gray bit in that position.
| Decimal | Binary | Gray | Decimal | Binary | Gray |
|---|---|---|---|---|---|
| 0 | 0000 | 0000 | 8 | 1000 | 1100 |
| 1 | 0001 | 0001 | 9 | 1001 | 1101 |
| 2 | 0010 | 0011 | 10 | 1010 | 1111 |
| 3 | 0011 | 0010 | 11 | 1011 | 1110 |
| 4 | 0100 | 0110 | 12 | 1100 | 1010 |
| 5 | 0101 | 0111 | 13 | 1101 | 1011 |
| 6 | 0110 | 0101 | 14 | 1110 | 1001 |
| 7 | 0111 | 0100 | 15 | 1111 | 1000 |
Example 3: binary 1011 to Gray and back.
- Copy the first bit: 1.
- 1 ⊕ 0 = 1; 0 ⊕ 1 = 1; 1 ⊕ 1 = 0. Gray = 1110.
- Back again: copy the first bit, b3 = 1. Then b2 = 1 ⊕ 1 = 0, b1 = 0 ⊕ 1 = 1, b0 = 1 ⊕ 0 = 1. Binary = 1011.
In the table, decimal 11 appears with Gray 1110: it matches.
Common mistakes
- Grouping bits for octal or hexadecimal starting from the left. Grouping always starts at the least significant bit.
- Forgetting the "+1" of 2's complement. Inverting alone gives the 1's complement, which has two zeros and a different range.
- Reading 11010011 as 211 when the register is signed. The same bit pattern is 211 unsigned or −45 signed: it depends on the convention.
- Confusing BCD with binary. 25 in BCD is 0010 0101, whereas in binary it is 11001.
- Converting Gray to binary by XORing with the neighboring Gray bit instead of the binary bit just computed.
Try it in the simulator
You can watch these representations "live" with a few inputs and outputs, and check the conversion to Gray code with XOR gates.
- Place four inputs as bits b3 to b0 and connect a display or four LEDs to see the value.
- Add three XOR gates: g2 = b3 ⊕ b2, g1 = b2 ⊕ b1, g0 = b1 ⊕ b0, and wire b3 straight to g3.
- Step the inputs from 0000 to 1111 and watch on the outputs that only one Gray bit changes between one value and the next.
- Generate the truth table of the circuit and compare it with the table in this article.
Exercises
- Convert 110101102 to hexadecimal and to decimal.
Answer
1101 0110 = D616 = 13·16 + 6 = 214.
- Convert 200 to binary and to hexadecimal.
Answer
200 = 128 + 64 + 8 = 110010002 = C816.
- Represent −100 in 8-bit 2's complement.
Answer
100 = 01100100; inverted 10011011; plus 1: 10011100. Check: −128 + 16 + 8 + 4 = −100.
- Convert decimal 6 to 4-bit Gray code and the BCD value 0111 0010 to decimal.
Answer
6 = 0110; Gray: 0, 0 ⊕ 1 = 1, 1 ⊕ 1 = 0, 1 ⊕ 0 = 1 → 0101. The BCD value 0111 0010 is 72.
To see how a circuit adds and subtracts these numbers, continue with binary adders.