BCD

The gist of coding decimal values in binary (Binary Coded Decimals) is to write each decimal value as an individual binary string. For example, 8 would become 1000. 10 would become 0001 and 0000 (1 and then 0). 15 would become 0001 and 0101 (1 and then 5). Recall this table:

Binary Denary
0000 0
0001 1
0010 2
0011 3
0100 4
0101 5
0110 6
0111 7
1000 8
1001 9

Note that the maximum value of a BCD string is 9. Above that, you have to split the value into individual values and convert from there.

Storing BCD values.

  1. Each value goes in individual registers

    If we have 0001 and 0101 (15), we could store these values in individual bytes (or registers for that matter (I'm using bytes for simplicity's sake)). We end up with 0000001 and 00000101.

  2. Two 4-bit codes are stored in one byte

    If we have a larger value, like 8503, this would be stored in 2 bytes. 8 and 5 goes in one, 0 and 3 go in the other. We would end up with 10000101 and 00000011.

BDC Addition and Subtraction

This is the same as regular ol' binary addition. The difference in procedure here occurs when your sum is a value greater than 9.

Take 8 + 7, or 1000 plus 0111. Let's add these together: 1000 + 0111 = 1111 (15), but in regular binary. In BCD, we add a correction factor of 6 to these sums. So now we do 1111 + 0110 to get 10101, or 00010101 in 8 bits. We can check this by converting 15 into BCD: 1 becomes 0001 and 5 becomes 0101. Put together, we get 00010101.

This article was written on 12/09/2023. If you have any thoughts, feel free to send me an email with them. Have a nice day!