Electrical Engineering › Section R · Digital concepts, number systems, and codes › Topic R.6
Hexadecimal, octal, BCD, and error codes
Long strings of bits are hard for people to read, and plain binary is not always the most useful code. This topic covers hexadecimal and octal shorthand, binary-coded decimal, the Gray code, ASCII and Unicode text, and the parity and CRC codes that detect transmission errors.
Hexadecimal: four bits per digit
Hexadecimal, base 16, needs sixteen digits: 0 to 9, then A to F for ten to fifteen. Because \(16 = 2^4\), each hex digit stands for exactly four bits, which makes hex a compact way to write binary. To convert binary to hex, split the bits into groups of four starting from the right (pad the leftmost group with 0s) and replace each group by its digit. To convert hex to binary, replace each digit by its four bits. So \(2F_{16} = 0010\,1111_2\), and an 8-bit byte is always exactly two hex digits. Some software writes hex with a prefix, 0x2F, or a suffix, 2Fh.
Worked example: hex conversions
Convert \(101101101110_2\) to hex, then to decimal, then back. 1. Group from the right: 1011 0110 1110, which is B, 6, E, so the number is \(B6E_{16}\). 2. Hex weights are powers of 16: 256, 16, 1. So
3. Back to hex by repeated division by 16: 2926 gives 182 remainder 14 (E); 182 gives 11 remainder 6; 11 gives 0 remainder 11 (B). 4. Read the remainders from last to first: \(B6E_{16}\). Check: three hex digits hold 12 bits, as the original had.
Hex arithmetic
Hex addition works column by column like decimal, carrying 16 instead of ten. For \(3A_{16} + 2F_{16}\): in the units column \(A + F = 10 + 15 = 25 = 16 + 9\), so write 9 and carry 1; in the sixteens column \(3 + 2 + 1 = 6\). The sum is \(69_{16}\), and in decimal \(58 + 47 = 105 = 6 \times 16 + 9\). Memory addresses, colour values such as FF8000 and machine instructions are routinely written in hex, so quick hex addition is a practical skill.
Octal: three bits per digit
Octal, base 8, uses the digits 0 to 7, with weights 1, 8, 64, 512. Because \(8 = 2^3\), each octal digit stands for three bits, so binary converts by grouping in threes from the right. For \(1011011101_2\), the groups are 1, 011, 011, 101, giving \(1335_8\). Check in decimal:
and the binary sums to 733 too. Octal was common on early computers with 12- or 36-bit words; today you meet it mainly in Unix-style file permissions, such as 755.
Binary-coded decimal
Binary-coded decimal (BCD) codes each decimal digit separately in four bits, using the 8421 weights. So \(947_{10}\) becomes 1001 0100 0111 in BCD. Only the codes 0000 to 1001 are used; 1010 to 1111 are invalid in BCD. BCD is less compact than binary, since 947 needs 12 BCD bits but only 10 binary bits (1110110011), but it converts directly to and from decimal digits. That suits displays, keypads, clocks and calculators, where each digit is shown or entered on its own, and financial systems where decimal rounding must be exact.
Worked example: BCD addition
Add 58 and 67 in BCD. The rule: add each digit group in binary; if the result is more than 1001 or produces a carry, add 0110 to correct it. 1. Units: \(1000 + 0111 = 1111\), which is 15, more than 9. Add 0110: \(1111 + 0110 = 1\,0101\), so the units digit is 0101 (5) with a carry of 1. 2. Tens: \(0101 + 0110 + 1 = 1100\), which is 12. Add 0110: \(1\,0010\), so the tens digit is 0010 (2) with a carry of 1. 3. The carry becomes the hundreds digit, 0001. Result: 0001 0010 0101, which is 125. Check: \(58 + 67 = 125\). Adding 6 skips the six invalid codes.
The Gray code
In the Gray code, successive values differ in exactly one bit: the 3-bit sequence is 000, 001, 011, 010, 110, 111, 101, 100. Shaft-position encoders use it because, as a disc turns, several bits of plain binary may change at slightly different moments and briefly show a wildly wrong value, whereas Gray code can only be off by one step. To convert binary to Gray, keep the MSB, then make each next Gray bit the XOR of that binary bit and the one to its left: 1011 becomes 1110. To convert back, each binary bit is the XOR of the previous binary bit and the current Gray bit: Gray 1101 becomes binary 1001.
ASCII and Unicode
Text needs an alphanumeric code. ASCII, the American Standard Code for Information Interchange, uses 7 bits for 128 characters. Codes 0 to 31, and 127, are control characters such as line feed and escape; the rest are printable. Capital A is \(41_{16}\) (65), lower-case a is \(61_{16}\), and the digit 0 is \(30_{16}\), so a digit's code is its value plus \(30_{16}\). Various 8-bit extensions add 128 more characters. Unicode gives a number to every character of the world's writing systems; its common UTF-8 encoding stores ASCII characters as the same single bytes, so plain ASCII text is valid UTF-8.
Parity bits
Bits can be corrupted by noise on the way. The simplest check adds a parity bit so that the total number of 1s is even (even parity) or odd (odd parity). The ASCII code for C, 1000011, has three 1s, so with even parity the sender adds a 1 and transmits 11000011. The receiver counts the 1s; an odd count reveals an error. Parity detects any single-bit error, indeed any odd number of errors, but misses two errors in the same word, and it cannot say which bit is wrong. Hamming codes add several check bits so that a single wrong bit can be located and corrected.
Worked example: a CRC check
A cyclic redundancy check (CRC) divides the data by a generator pattern using modulo-2 arithmetic, in which subtraction is XOR with no borrows, and sends the remainder with the data. Data 1101, generator 1011. 1. Append three 0s, one fewer than the generator's length: 1101000. 2. Divide: \(1101 \oplus 1011 = 0110\); bring down a bit, \(1100 \oplus 1011 = 0111\); then \(1110 \oplus 1011 = 0101\); then \(1010 \oplus 1011 = 0001\). 3. The remainder is 001, so transmit 1101001. 4. The receiver divides 1101001 by 1011 and gets remainder 000: no error detected. Check: if the second bit flips (1111001), the remainder becomes 110, flagging the error.
Common mistakes to avoid
First, grouping bits from the left when converting to hex or octal; always start at the binary point. Second, treating BCD as binary: 0001 0010 in BCD is 12, but as binary it is 18. Third, forgetting the 0110 correction in BCD addition when a digit group exceeds 1001 or carries. Fourth, assuming parity finds every error; two flipped bits cancel out. Fifth, reading hex digits A to F as single decimal digits: \(1A_{16}\) is 26, not 110.
Key takeaways
Hex uses digits 0 to F, each worth four bits; octal uses 0 to 7, each worth three; both convert by grouping bits from the right. Convert to decimal by weights (16ⁿ or 8ⁿ) and back by repeated division. BCD codes each decimal digit in 8421 binary, with 1010 to 1111 invalid, and corrects additions by adding 0110. The Gray code changes one bit per step, which suits shaft encoders. ASCII is a 7-bit text code, extended by Unicode and UTF-8. Parity detects odd numbers of bit errors; CRCs use modulo-2 division and catch far more.
Flashcards
Flashcard
0–9 and A–F, for zero to fifteen
Test your understanding
Question 1 of 5
What is 1110101100₂ in hexadecimal?
Question 2 of 5
What is 2F₁₆ in decimal?
Question 3 of 5
In BCD addition, the units digits give 0110 + 0111 = 1101. What happens next?
Question 4 of 5
Which code changes exactly one bit between each pair of successive values?
Question 5 of 5
A word sent with even parity arrives as 10110110. What does the receiver conclude?
