BCD converter: decimal to BCD and back
BCD (binary-coded decimal) writes each decimal digit as its own 4-bit binary number, so 1995 becomes 0001 1001 1001 0101. Type a number below to convert decimal to BCD, BCD to decimal, or binary to BCD with every double dabble step.
The BCD and excess-3 codes
Only ten of the sixteen 4-bit patterns are used. The six codes 1010 to 1111 (10 to 15) never appear in valid BCD, which is why the converter rejects them.
| Decimal digit | BCD (8421) | Excess-3 |
|---|---|---|
| 0 | 0000 | 0011 |
| 1 | 0001 | 0100 |
| 2 | 0010 | 0101 |
| 3 | 0011 | 0110 |
| 4 | 0100 | 0111 |
| 5 | 0101 | 1000 |
| 6 | 0110 | 1001 |
| 7 | 0111 | 1010 |
| 8 | 1000 | 1011 |
| 9 | 1001 | 1100 |
The name 8421 gives the weight of each bit, as in ordinary binary. Excess-3 adds 3 to every digit first; its bits invert to the 9's complement of the digit. Both are built as gate circuits on this site: BCD to excess-3 and excess-3 to BCD.
BCD vs binary
Binary converts the whole number to base 2: 1995 is 11111001011, 11 bits. BCD converts digit by digit: 16 bits for the same number. BCD wastes some space, but each group maps straight to a decimal digit, so it suits anything that shows or keeps decimal numbers: seven-segment displays, clocks, calculators and money, where decimal fractions like 0.1 must be exact. Real-time clock chips such as the DS1307 keep the time in BCD registers. For base conversion without BCD, use the number base converter.
Binary to BCD: the double dabble method
To turn binary into BCD in hardware or code, shift the binary number left one bit at a time into a row of BCD digits. Before each shift, add 3 to any digit that is 5 or more, so that the doubling carries into the next digit instead of making a code above 9. Here is 13 (1101):
| Step | Tens | Ones | Binary left |
|---|---|---|---|
| start | 0000 | 0000 | 1101 |
| shift left | 0000 | 0001 | 101 |
| shift left | 0000 | 0011 | 01 |
| shift left | 0000 | 0110 | 1 |
| ones is 6, so add 3 | 0000 | 1001 | 1 |
| shift left | 0001 | 0011 | (done) |
After four shifts, one per bit, the digits read 0001 0011: 1 and 3. The converter above runs the same steps for up to 32 bits, and its code is tested on every 16-bit number. A 4-bit version is built from gates in the binary to BCD converter circuit.
BCD arithmetic
Adding two BCD digits in a plain binary adder can produce 10 to 19, which are not BCD codes. The fix is to add 6 (0110) whenever the sum is above 9, which skips the six unused codes and carries 1 into the next digit: 7 + 5 = 1100, plus 6 is 1 0010, a carry and 2, so 12. See it working in the BCD adder circuit, and read the BCD lesson for the full story. To decode a BCD digit to one of ten lines, see the BCD to decimal decoder; to go the other way, the decimal to BCD encoder.
Frequently asked
How do you convert decimal to BCD?
Replace each decimal digit with its own 4-bit binary code: 0 is 0000, 1 is 0001 and so on up to 9, which is 1001. So 1995 becomes 0001 1001 1001 0101. Unlike ordinary binary, you never convert the whole number at once, only one digit at a time.
How do you convert BCD to decimal?
Split the bits into groups of 4 from the right (from the decimal point, if there is one), and read each group as one decimal digit: 0010 0100 is 24. A group from 1010 to 1111 is not valid BCD, because 10 to 15 are not decimal digits.
What is the difference between BCD and binary?
Binary writes the whole number in base 2, so 13 is 1101. BCD writes each decimal digit separately, so 13 is 0001 0011. BCD needs more bits, 16 for 1995 against 11 in binary, but it converts to and from decimal digits with no arithmetic, which suits displays and money.
How do you convert binary to BCD?
Use the double dabble method: shift the binary number left, one bit at a time, into a row of 4-bit BCD digits, and before each shift add 3 to any digit that is 5 or more. After as many shifts as there are bits, the BCD digits hold the decimal value. The binary to BCD mode above shows every step.
Why add 3 in double dabble?
A digit of 5 or more becomes 10 or more when the next shift doubles it, which is no longer a BCD digit. Adding 3 first means the shift adds 6 in total, and adding 6 is exactly what carries a 4-bit group past 9 into the next digit, the same correction a BCD adder makes.
What is excess-3 code?
Excess-3 writes each decimal digit plus 3 in 4 bits, so 0 is 0011 and 9 is 1100. It is self-complementing: inverting all four bits gives the 9's complement of the digit, which made decimal subtraction simpler in early machines.
What is packed BCD?
Packed BCD stores two digits in each byte, one per 4-bit half, so 1995 is the two bytes 0x19 0x95. Unpacked BCD uses a whole byte per digit, 0x01 0x09 0x09 0x05. The x86 instruction set has DAA, decimal adjust after addition, for packed BCD, although it is not available in 64-bit mode.
BCD is the bridge between the binary inside a computer and the decimal digits people read. Read the BCD lesson, or open the lab and build a converter from gates.
Related tools: number base converter, binary to decimal converter, Gray code converter and binary calculator.
Open the lab →