BCD and hexadecimal in practice
Pure binary is the most compact way to store an integer, but it is not always the most useful. Binary Coded Decimal (BCD) stores each denary digit separately, which suits devices that display or calculate in decimal. Hexadecimal is a human-friendly shorthand for long binary patterns. The syllabus asks you to convert to and from BCD and to describe practical applications of both, so Paper 1 questions here are usually a conversion plus a "describe one use" mark.
Binary Coded Decimal
Binary Coded Decimal (BCD) is a representation in which each denary digit of a number is stored separately as its own 4-bit binary code.
To write a number in BCD, take each denary digit in turn and replace it by its 4-bit binary value.
| Denary digit | BCD code |
|---|---|
| 0 | 0000 |
| 1 | 0001 |
| 2 | 0010 |
| 3 | 0011 |
| 4 | 0100 |
| 5 | 0101 |
| 6 | 0110 |
| 7 | 0111 |
| 8 | 1000 |
| 9 | 1001 |
So in BCD is 0010 1001 0100 0111: the digits 2, 9, 4 and 7, one nibble each.
Only ten of the sixteen 4-bit patterns are used. The six patterns 1010 to 1111 (10 to 15) are invalid in BCD, because no single denary digit has those values. A BCD decoder that meets one of them knows the data is corrupt.
Packed and unpacked BCD
- In packed BCD, two digits share one byte, one in each nibble: is stored as the byte
0101 1001. - In unpacked BCD, each digit has a whole byte, with the upper nibble set to zero: is
00000101 00001001.
Packed BCD saves space; unpacked BCD is simpler to process one digit at a time.
BCD compared with pure binary
BCD needs more bits than pure binary. needs 16 bits in BCD but only 12 bits in binary (101110000011), because BCD wastes six of the sixteen patterns in every nibble. In return, each digit can be extracted, displayed or rounded without a binary-to-denary conversion.
To convert denary to BCD: replace each denary digit by its 4-bit code.
To convert BCD to denary: split into nibbles from the left, and replace each nibble by its digit.
BCD is not the same as converting the whole number to binary: in BCD is 0101 1001, but in binary is 00111011.
Write the denary number in BCD, and write in 16-bit binary for comparison.
Solution
BCD: , , , giving 0111 0000 0101.
Binary: , giving 0000001011000001.
The two patterns are completely different: BCD codes the digits, binary codes the value.
A digital meter stores readings in packed BCD. Two readings are 1001 0011 0110 and 0100 1100 0010. Convert each to denary, or explain why it cannot be converted.
Solution
1001 0011 0110: , , , so the reading is 936.
0100 1100 0010: the middle nibble is 1100, which is 12. A single denary digit cannot be 12, so this is not a valid BCD value: the data has been corrupted or is not BCD.
Why use BCD? Practical applications
The underlying reason for every BCD application is the same: the data is naturally decimal, so storing it digit by digit avoids conversion and avoids rounding errors.
- Digital displays: clocks, calculators, meters, scales, fuel pumps. Each digit drives one seven-segment display. With BCD, each nibble can be sent straight to a display driver chip. Converting a pure binary value into separate digits would need repeated division by 10 every time the display updates.
- Financial and currency calculations. Many decimal fractions, such as , cannot be represented exactly in binary, just as cannot be written exactly in decimal. A program adding in binary floating point typically gets . In banking, every cent must be exact, so decimal (BCD-style) arithmetic is used to avoid these rounding errors.
- Real-time clock chips. The clocks inside computers and embedded devices commonly store seconds, minutes, hours and dates in BCD, because the values are shown to humans in decimal.
- Electronic instruments that read a decimal value from a keypad or show a decimal reading (digital voltmeters, petrol pump displays).
This is not explicitly required by the syllabus but helps you see the cost of BCD. When two BCD digits are added and the result is greater than 9 (or produces a carry), the hardware adds 0110 (6) to skip the six invalid patterns. For : the units nibbles give (13), which is invalid, so add 0110 to get 1 0011: write 3, carry 1. The tens give (8). The answer is 1000 0011, which is 83. That extra correction step is why BCD arithmetic is slower than pure binary.
Hexadecimal in practice
Hexadecimal is never stored by a computer as anything other than binary. It is used wherever people have to read, write or remember binary values, because:
- one hex digit is exactly four bits, so conversion is instant and exact;
- it is much shorter: a byte is two hex digits instead of eight bits;
- it is far less error-prone to copy than long strings of 0s and 1s;
- it still shows the bit structure, which denary hides.
Applications of hexadecimal
- Memory addresses and memory dumps. Debuggers and error reports show the contents of memory in hex, for example
&3A7F, so programmers can inspect values and addresses. - Colour codes in HTML and CSS. A 24-bit colour is written
#RRGGBB, two hex digits each for red, green and blue:#FF8000is full red, half green, no blue (orange). - MAC addresses. The 48-bit hardware address of a network interface is written as twelve hex digits in six pairs, for example
00:1A:2B:3C:4D:5E. - Assembly language and machine code. Opcodes and operands are listed in hex; Cambridge assembly uses
&to mark a hex operand, as inADD &4A. - Error and status codes, such as the hex codes in operating system crash screens.
- IPv6 addresses, written as eight groups of four hex digits (see the internet and IP addresses).
A web page uses the colour #4B0082. Give the denary value of each colour component and state which component is strongest.
Solution
Split into pairs: 4B, 00, 82.
- Red:
- Green:
- Blue:
Blue (130 out of 255) is the strongest component. With no green, this is a deep violet (indigo).
A technician says "the processor stores the value in hexadecimal". Explain why this is incorrect, and give two reasons why the value is displayed to the technician in hexadecimal.
Solution
All data in a computer is stored in binary; hexadecimal is only a way of writing the binary value for humans.
Reasons for displaying in hex:
- It is shorter: each hex digit replaces four bits, so a 32-bit value is 8 hex digits instead of 32 bits, which is easier to read and remember.
- It is less likely to be miscopied, and conversion to binary is direct (one digit to four bits), so the technician can still see individual bits when needed, which denary does not allow.
A shop's electronic price display shows prices up to 9999 cents. Explain why the display controller might store prices in BCD rather than pure binary, and state one drawback.
Solution
Benefit: each BCD nibble corresponds to one digit on the display, so the controller can send each digit directly to its seven-segment driver without converting from binary to denary. Prices are exact decimal values, so there are no rounding problems.
Drawback: BCD uses more memory. 9999 needs 16 bits in BCD but only 14 bits in pure binary (). BCD arithmetic is also slower, because results need correcting.
Converting the whole number when BCD is asked for. BCD codes each digit separately. in BCD is 0010 0101, not 00011001.
Forgetting leading zeros in a nibble. Every BCD digit is exactly four bits: 3 is 0011, not 11. Without fixed nibbles the digits cannot be separated.
Saying hex is "stored" or "used by the CPU". Hex is a representation for people. Marks are given for "easier for humans to read/write/debug", never for "the computer uses it".
Vague applications. "Used in computers" earns nothing. Name the context: "the digits on a digital clock", "HTML colour codes", "memory dumps when debugging".
"Describe one practical application of BCD" usually carries two marks: one for naming a sensible use and one for explaining why BCD suits it (each digit drives a display directly; decimal values such as currency are exact). Do the same for hexadecimal: the use, then the reason (shorter and easier for a human to read, fewer errors).
In conversion questions, keep a space between nibbles in your answer: 1001 0011 0110. It makes your working obvious to the examiner and stops digits merging.
- BCD stores each denary digit as its own 4-bit code;
1010to1111are invalid. - Packed BCD: two digits per byte. Unpacked BCD: one digit per byte.
- BCD uses more bits than binary and arithmetic is slower, but digits are directly available and decimal values are exact.
- BCD applications: digital displays (clocks, calculators, meters), currency calculations, real-time clock chips.
- Hex is a shorthand for binary, one digit per nibble; it is easier for humans to read, write and debug.
- Hex applications: memory dumps and addresses, HTML colour codes, MAC addresses, assembly and machine code listings, error codes, IPv6.
Practice
- Convert to BCD.
- Convert the BCD value
0101 1001 0100 0111to denary. - Explain why
0111 1010is not a valid packed BCD value. - Write the denary number 59 in (a) packed BCD, (b) unpacked BCD, (c) 8-bit binary.
- Give two reasons why programmers view memory contents in hexadecimal rather than binary.
- The colour
#FF8000is used on a website. Give the denary value of the red, green and blue components. - Describe two applications of BCD, explaining why BCD is appropriate in each.
- A MAC address is 48 bits long. State how many hexadecimal digits are needed to write it, and how many bytes it occupies.
- A calculator stores numbers in packed BCD, using 4 bytes per number. (a) State the largest number it can store. (b) State the largest unsigned integer the same 4 bytes could store in pure binary, as a power of 2 expression. (c) Explain why the calculator designer might still choose BCD.
- A banking program written by a student adds 0.10 to an account balance ten times, but the final balance is printed as 0.9999999999999999 instead of 1.00. Explain the cause and describe how a decimal or BCD representation would avoid the problem.
Answers
0010 1001 0100 0111.- 5, 9, 4, 7: 5947.
- The second nibble
1010is ten. Each BCD nibble must represent a single denary digit 0 to 9, so1010is invalid. - (a)
0101 1001; (b)00000101 00001001; (c)00111011. - Hex is much shorter (two digits per byte), so it is easier to read and remember; it is less error-prone to copy; and it converts directly to binary, one digit per four bits, so individual bits can still be identified.
- Red , green , blue .
- For example: digital clock or calculator display, because each digit can be sent directly to a seven-segment display driver without conversion; currency calculations, because decimal fractions such as 0.10 are stored exactly, so no rounding errors occur.
- hex digits; bytes.
- (a) 8 digits: 99 999 999. (b) . (c) Each digit maps to one display digit with no conversion, and decimal arithmetic is exact, which matters for a calculator showing decimal results.
- 0.1 cannot be represented exactly in binary (it is a recurring binary fraction), so each addition stores a slightly wrong value and the errors accumulate. In decimal/BCD each digit, including those after the decimal point, is stored exactly, so ten additions of 0.10 give exactly 1.00. (Alternatively store the amount as an integer number of cents.)