kids encyclopedia robot

Binary-coded decimal facts for kids

Kids Encyclopedia Facts
Binary clock
A binary clock uses lights to show numbers in binary. This clock shows time using BCD, where each group of lights stands for a decimal digit.

In computers and electronic devices, binary-coded decimal (BCD) is a way to show decimal numbers using binary code. Each decimal digit (like 0, 1, 2, up to 9) is given its own set of bits, usually four or eight. Sometimes, special bit patterns are used to show if a number is positive or negative, or if there's an error.

BCD is good because it can show decimal numbers very accurately. This helps avoid tiny errors that can happen when computers convert numbers between decimal and pure binary. It also makes it easier to change numbers into a format that humans can read. However, BCD can make the circuits in computers a bit more complex. It also uses a little more storage space than pure binary numbers.

Many older computers used BCD, like the IBM System/360 series. Even today, BCD is important in areas like finance and business. This is because it helps keep calculations precise, which is very important for money.

How BCD Works

BCD uses four bits to represent each decimal digit from 0 to 9. The most common way to do this is called Natural BCD (NBCD) or "8421" encoding. This name comes from the "weight" of each bit: 8, 4, 2, and 1.

Here’s how each decimal digit is shown in 8421 BCD:

Decimal digit 8 4 2 1
0 0 0 0 0
1 0 0 0 1
2 0 0 1 0
3 0 0 1 1
4 0 1 0 0
5 0 1 0 1
6 0 1 1 0
7 0 1 1 1
8 1 0 0 0
9 1 0 0 1

Other ways of encoding BCD exist, like "4221" or "Excess-3". These use different rules for how the bits represent the numbers.

Unpacked and Packed BCD

Computers often handle data in 8-bit chunks called bytes. BCD numbers can be stored in two main ways:

  • Unpacked BCD: Each decimal digit gets its own full byte. Four bits in the byte hold the BCD digit, and the other four bits are usually empty (zeros).
  • Packed BCD: Two decimal digits are stored in a single byte. One digit is in the first four bits (called a nibble), and the other digit is in the second four bits.

Let's look at an example using the number 91:

In unpacked BCD, it would take two bytes: Decimal: 9 1 Binary : 0000 1001 0000 0001

In packed BCD, it fits into one byte: Decimal: 9 1 Binary : 1001 0001

Packed BCD saves space because it uses half as many bytes as unpacked BCD. For larger numbers, you just use more bytes. For example, the number 12345 in packed BCD (using big-endian format, which means the most important part comes first) would be: Decimal: 0 1 2 3 4 5 Binary : 0000 0001 0010 0011 0100 0101

The first digit is a zero to make sure there's an even number of digits for packing.

Packed BCD Details

In packed BCD, each group of four bits (nibble) is a decimal digit. This method has been used in IBM mainframe computers since the 1960s. Usually, the most important digits are at the beginning of the number. The last nibble of the last byte often shows if the number is positive or negative.

Common signs are 1100 (hex C) for positive and 1101 (hex D) for negative. For example, the number 127 would be 0001 0010 0111 1100 (127C). The number -127 would be 0001 0010 0111 1101 (127D). If a number is unsigned (no positive/negative sign), it might use 1111 (hex F).

Sign
digit
BCD
8 4 2 1
Sign Notes
A 1 0 1 0 +  
B 1 0 1 1  
C 1 1 0 0 + Preferred
D 1 1 0 1 Preferred
E 1 1 1 0 +  
F 1 1 1 1 + Unsigned

Even though packed BCD uses more memory than pure binary numbers, it's very easy to convert to text formats like ASCII or EBCDIC. This is because no complex math is needed for the conversion. This makes it useful for financial and business programs that need exact decimal calculations.

Programming languages like COBOL and PL/I support packed BCD. Some computer processors, like those in older VAX computers, also have special instructions to work with packed BCD directly.

Zoned Decimal

Some computer systems, especially IBM mainframes, use something called zoned decimal. In this system, each decimal digit is stored in one byte. The lower four bits of the byte hold the BCD digit. The upper four bits, called "zone" bits, are set to a specific value. This makes the byte represent a character that looks like the digit.

For example, on EBCDIC systems, the zone bits are 1111 (hex F). So, the digits 0 through 9 are stored as F0 to F9 (hex). These are the EBCDIC codes for the characters "0" through "9".

If a zoned decimal number has a sign, the zone bits of the last digit will show the sign. For example, if the hex bytes F1 F2 D3 represent a number, it means -123: F1 F2 D3 1 2 −3

BCD in Electronics

BCD is very common in electronic devices that need to display numbers, especially those without a main computer chip. Think of things like digital clocks or meters. Using BCD simplifies how numbers are handled for display.

Imagine building a device with several seven-segment displays (like those on a digital clock). If the numbers were stored in pure binary, it would take complex circuits to convert them for each display. But with BCD, each digit is already in a simple format that matches the display. This can make the whole system simpler to design.

Even small computers or microcontrollers often use BCD internally. This can make their programs smaller and faster, as converting between binary and decimal can take a lot of effort for these small chips. Many pocket calculators do all their math using BCD.

How to Do Math with BCD

Doing math with BCD is a bit different from regular binary math.

Addition

To add BCD numbers, you first add them as if they were binary. If the result for any digit is greater than 9, you need to "correct" it. You do this by adding 6 (which is 0110 in binary) to that digit's four bits. This extra 6 helps to create a "carry" to the next decimal place, just like when you carry over a 10 in regular decimal addition.

For example, adding 9 (1001) and 8 (1000): 1001 (9) +1000 (8)


10001 (17 in binary)

Since 10001 is greater than 9 (1001), we add 6 (0110) to it: 10001 + 0110


0001 0111 (This becomes 1 and 7 in BCD)

The result, 0001 0111, means 17 in BCD, which is correct.

Subtraction

Subtraction in BCD is often done using "ten's complement." This is similar to how computers use "two's complement" for binary subtraction. To subtract a number, you find its ten's complement and then add it.

For example, to subtract 432 from 357: 1. Find the ten's complement of 432. The nine's complement of 432 is 999 - 432 = 567. Then add 1 to get 568. 2. Represent 357 and 568 in BCD. 3. Add them together. If any digit's sum is invalid (greater than 9), add 6 to correct it and carry over. 4. The sign of the result is handled separately, often by a special sign bit or nibble.

BCD vs. Pure Binary

Both BCD and pure binary have their pros and cons.

Advantages of BCD

  • Accuracy: Many decimal numbers, like 0.2, can't be perfectly represented in binary. BCD keeps them exact, which is vital for financial calculations.
  • Easy Conversion: Changing BCD numbers into text or for display on screens is very simple. Each digit is already in a format that can be directly mapped.
  • Decimal Rounding: Rounding numbers to a specific decimal place is easier with BCD.

Disadvantages of BCD

  • More Complex Circuits: Adding and subtracting BCD numbers needs more complex electronic circuits than pure binary.
  • More Storage: BCD usually needs more memory space than pure binary to store the same numbers. A four-bit BCD digit uses about 20% more space than a binary number.
  • Slower Operations: On most modern computers, BCD math can be slower because the main processor often doesn't have built-in instructions for BCD. It's usually done by software.

Real-World Uses of BCD

  • The BIOS (Basic Input/Output System) in many personal computers stores the date and time in BCD. This is because older real-time clock chips used BCD.
  • The MOS Technology 6502 processor, used in many old computers like the Apple II and Commodore 64, had a special BCD mode for its addition and subtraction instructions.
  • Early models of the PlayStation 3 stored the date and time in BCD. This caused a big problem in 2010 when the year was misinterpreted, leading to a worldwide outage for many consoles. This event is sometimes called the Year 2010 problem.
kids search engine
Binary-coded decimal Facts for Kids. Kiddle Encyclopedia.