Calculate packed 16-bit BCD from four decimal digits or decode a binary or hex BCD value back into the original digits for 4-digit numbers.
BCD Formula
BCD, or binary-coded decimal, stores each decimal digit as its own 4-bit binary group. This calculator uses packed 16-bit BCD for four decimal digits, so D1, D2, D3, and D4 are stored as four nibbles.
Encode four digits to packed BCD:
BCD = D_1*16^3 + D_2*16^2 + D_3*16 + D_4
Equivalent binary grouping:
BCD_{binary} = bin4(D_1)\ bin4(D_2)\ bin4(D_3)\ bin4(D_4)Decode packed BCD back to digits:
D_1 = floor(BCD / 16^3)
D_2 = floor(BCD / 16^2) mod 16
D_3 = floor(BCD / 16) mod 16
D_4 = BCD mod 16
- BCD is the packed 16-bit binary-coded decimal value.
- D1 is the thousands digit.
- D2 is the hundreds digit.
- D3 is the tens digit.
- D4 is the ones digit.
- bin4(D) means the 4-bit binary form of one decimal digit.
- mod gives the remainder after division.
To encode, enter all four decimal digits. Each digit must be from 0 to 9. The calculator converts each digit into a 4-bit BCD nibble, joins the nibbles in order, and also shows the equivalent hexadecimal packed value.
To decode, enter a packed BCD value in binary or hexadecimal. The calculator splits the 16-bit value into four nibbles and reads each nibble as one decimal digit. Any nibble greater than 9 is invalid BCD.
Decimal Digits and 4-Bit BCD Codes
| Decimal digit | 4-bit BCD | Hex nibble |
|---|---|---|
| 0 | 0000 | 0 |
| 1 | 0001 | 1 |
| 2 | 0010 | 2 |
| 3 | 0011 | 3 |
| 4 | 0100 | 4 |
| 5 | 0101 | 5 |
| 6 | 0110 | 6 |
| 7 | 0111 | 7 |
| 8 | 1000 | 8 |
| 9 | 1001 | 9 |
Common Packed BCD Examples
| Decimal digits | Packed BCD binary | Packed BCD hex |
|---|---|---|
| 0000 | 0000 0000 0000 0000 | 0x0000 |
| 1234 | 0001 0010 0011 0100 | 0x1234 |
| 5078 | 0101 0000 0111 1000 | 0x5078 |
| 9999 | 1001 1001 1001 1001 | 0x9999 |
Example Problems
Example 1: Encode 1011 into packed BCD
Use D1 = 1, D2 = 0, D3 = 1, and D4 = 1.
- D1 = 1 becomes 0001
- D2 = 0 becomes 0000
- D3 = 1 becomes 0001
- D4 = 1 becomes 0001
The packed BCD result is 0001 0000 0001 0001, which is 0x1011.
Example 2: Decode 0x2468 from packed BCD
Split the hexadecimal value into nibbles:
- 2 becomes digit 2
- 4 becomes digit 4
- 6 becomes digit 6
- 8 becomes digit 8
The decoded decimal digits are 2468. In binary, the packed BCD value is 0010 0100 0110 1000.
FAQ
What is BCD?
BCD means binary-coded decimal. Instead of converting the whole decimal number into normal binary, BCD converts each decimal digit separately. For example, the decimal digits 1234 become 0001 0010 0011 0100 in packed BCD.
Why is 0x1011 decoded as 1011 and not as decimal 4113?
In packed BCD, each hexadecimal digit represents one decimal digit. So 0x1011 is read as the nibbles 1, 0, 1, and 1. That gives the decimal digits 1011. If you treated 0x1011 as an ordinary hexadecimal number, its decimal value would be 4113, but that is not the BCD interpretation.
Why are A through F invalid in BCD?
Each BCD nibble must represent a decimal digit from 0 to 9. Hexadecimal values A, B, C, D, E, and F represent 10 through 15, which are not single decimal digits. A packed BCD value such as 0x12A4 is invalid because the A nibble is greater than 9.
