BCD and hexadecimal in practice

AS · 10 min

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

Definition

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 digitBCD code
00000
10001
20010
30011
40100
50101
60110
70111
81000
91001

So 29472947 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: 5959 is stored as the byte 0101 1001.
  • In unpacked BCD, each digit has a whole byte, with the upper nibble set to zero: 5959 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. 29472947 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.

Key result

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: 5959 in BCD is 0101 1001, but 5959 in binary is 00111011.

Denary to BCD

Write the denary number 705705 in BCD, and write 705705 in 16-bit binary for comparison.

Solution

BCD: 7→01117 \to 0111, 0→00000 \to 0000, 5→01015 \to 0101, giving 0111 0000 0101.

Binary: 705=512+128+64+1705 = 512 + 128 + 64 + 1, giving 0000001011000001.

The two patterns are completely different: BCD codes the digits, binary codes the value.

BCD to denary and spotting invalid codes

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: 1001=91001 = 9, 0011=30011 = 3, 0110=60110 = 6, 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 0.10.1, cannot be represented exactly in binary, just as 13\tfrac{1}{3} cannot be written exactly in decimal. A program adding 0.1+0.20.1 + 0.2 in binary floating point typically gets 0.300000000000000040.30000000000000004. 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).
Extension: BCD addition

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 38+4538 + 45: the units nibbles give 1000+0101=11011000 + 0101 = 1101 (13), which is invalid, so add 0110 to get 1 0011: write 3, carry 1. The tens give 0011+0100+1=10000011 + 0100 + 1 = 1000 (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: #FF8000 is 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 in ADD &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).
Reading an HTML colour code

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: 4B16=4×16+11=754B_{16} = 4 \times 16 + 11 = 75
  • Green: 0016=000_{16} = 0
  • Blue: 8216=8×16+2=13082_{16} = 8 \times 16 + 2 = 130

Blue (130 out of 255) is the strongest component. With no green, this is a deep violet (indigo).

Justifying hexadecimal

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:

  1. 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.
  2. 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.
Choosing between binary and BCD

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 (214=16 3842^{14} = 16\ 384). BCD arithmetic is also slower, because results need correcting.

Watch out

Converting the whole number when BCD is asked for. BCD codes each digit separately. 2525 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".

Exam tip

"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.

Summary
  • BCD stores each denary digit as its own 4-bit code; 1010 to 1111 are 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

Question
  1. Convert 29472947 to BCD.
  2. Convert the BCD value 0101 1001 0100 0111 to denary.
  3. Explain why 0111 1010 is not a valid packed BCD value.
  4. Write the denary number 59 in (a) packed BCD, (b) unpacked BCD, (c) 8-bit binary.
  5. Give two reasons why programmers view memory contents in hexadecimal rather than binary.
  6. The colour #FF8000 is used on a website. Give the denary value of the red, green and blue components.
  7. Describe two applications of BCD, explaining why BCD is appropriate in each.
  8. A MAC address is 48 bits long. State how many hexadecimal digits are needed to write it, and how many bytes it occupies.
  9. 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.
  10. 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
  1. 0010 1001 0100 0111.
  2. 5, 9, 4, 7: 5947.
  3. The second nibble 1010 is ten. Each BCD nibble must represent a single denary digit 0 to 9, so 1010 is invalid.
  4. (a) 0101 1001; (b) 00000101 00001001; (c) 00111011.
  5. 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.
  6. Red FF=255FF = 255, green 80=12880 = 128, blue 00=000 = 0.
  7. 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.
  8. 48÷4=1248 \div 4 = 12 hex digits; 48÷8=648 \div 8 = 6 bytes.
  9. (a) 8 digits: 99 999 999. (b) 232−12^{32} - 1. (c) Each digit maps to one display digit with no conversion, and decimal arithmetic is exact, which matters for a calculator showing decimal results.
  10. 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.)

How well do you know this?

Builds on

Where this leads

Console

Search notes, courses and tools, or run an action