Binary Coded Decimal (BCD) | |||||||||||||||||||||||||
Sometimes it is important to know which column a denary digit is. For example if its in the tens column or the hundreds. We may not wish to do any calculations on this number. For example a product code. We may not wish to add or subtract product codes but we may wish to look at each part of the code separately. Consider the following code.
So the number 24587 will have
If we stored the above number as a binary number, 110000000001011 , then how can we tell that it has a country code of 2? It is very difficult to get that information out of that binary code. As such we could store it as a string of characters. Each character in a string is one byte long. So to store the above code we will need 5 bytes. This seems inefficient as the biggest digit we will store in each byte is 9. Why not use a single hexadecimal value to store it? A hexadecimal value requires just four bits to store itself. As such we can store two hexadecimal values per byte and as we have seen, it is very simple to get the decimal values from these hexadecimal values. The binary coded decimal effectively stores each digit as 4 bits. Lets work through our example.
Converting these values to binary we get -
As such the fully coded version will be - 0010 0100 0101 1000 0111 |