What is BCD?
Binary Coded Decimal is a system of writing numbers that assigns a 4-digit binary code to each number 0 to 9 in a decimal (base 10) number. In other words, Binary Coded Decimal is a way to convert decimal numbers to binary. However, Binary Coded Decimal is not the same as simple binary representation.
Binary Coded Decimal
Binary Coded Decimal, or BCD, is another way to convert decimal numbers to binary.
- It’s a binary encoding where each digit in a decimal number is represented as bits.
- Can be done in 4-bit or 8-bit (4-bit is usually preferred).
- Faster and more efficient than the existing binary system to convert decimal numbers to binary.
- Used in digital displays where data manipulation is a big task.
- So BCD plays a role here because data is being manipulated as if each digit is a separate single sub-circuit.
The BCD of a decimal number is written by replacing each decimal digit in the integer and fractional parts with its 4-bit binary equivalent.
Many decimal values have an infinite place-value representation in binary but have a finite place-value in Binary Coded Decimal. For example, 0.2 in binary is .001100… and in BCD is 0.0010. It avoids fractional errors and is also used in huge financial calculations.
Consider the following truth table and focus on how these are represented.
Truth Table for Binary Coded Decimal
In the BCD numbering system, the given decimal number is segregated into chunks of four bits for each decimal digit within the number. Each decimal digit is converted into its direct binary form (usually represented in 4-bits).
More about Binary Coded Decimal
In Binary Coded Decimal, each digit in a decimal base 10 number is represented as a group of four binary digits, or bits. Any base 10 number or digit can be represented in binary notation using Binary Coded Decimal.
Each digit is encoded separately. The full number is first segregated into its individual digits. These digits are then represented by their equivalent 4-bit Binary Coded Decimal codes as shown in this truth table.
In general Binary to Decimal using Binary Coded Decimal can be done using 4-bit or 8-bit. But 4-bit is more preferred which represents decimal values from 0 to 9.
Arithmetic operations like multiplication and addition are required to convert real binary numbers to decimal. But in Binary Coded Decimal only 10 different combinations are possible (see above table) in each 4-bit binary sequence. So Binary Coded Decimal is an easier way to represent decimal numbers.
Example: -
Decimal number = 1764
The Binary Coded Decimal rendition is represented as the following:
The binary decimal representation of a number is not the same as the binary representation. For example the decimal 1895 is 11101100111 in binary and 0001100010010101 in binary decimal.
The binary for each of the above always goes from left to right. Other bit patterns are used in binary decimal to represent special characters for a system, like sign (positive or negative)
Advantages and applications of Binary Coded Decimal
Binary Coded Decimal gets around the size limitations of integer arithmetic and allows for easy conversion between machine and human readable numbers. It’s easier to code and decode than binary, so BCD is a quick way to convert decimal to binary.
BCD is good for digital displays where large numbers are hard to handle or display.
Since Binary Coded Decimal treats each digit as a separate subcircuit, data manipulation in such devices is easier. There are integrated circuits that can give a Binary Coded Decimal output on such displays.
The Binary Coded Decimal conversion system is also used in some currency applications where floating-point representations are not completely accurate.
Limitations of Binary Coded Decimal
BCD has some downsides. For example, representing a decimal number as BCD requires extra bits in a computer so it’s not very efficient. It also adds more circuit complexity compared to the standard binary system. BCD is also wasteful since many 4-bit states (10 to 16) are not used.
Another limitation of BCD is that doing arithmetic with BCD numbers is cumbersome since no digit can exceed 9. So adding two decimal digits in BCD will create a carry of 1 which must be added to the next group of 4 bits to get the correct BCD representation.
Types of Binary Coded Decimal
There are two types of Binary Coded Decimal.
Unpacked Binary Coded Decimal
In unpacked Binary Coded Decimal numbers, each 4-bit Binary Coded Decimal group is stored in a separate register inside a computer. The drawback here is that, if the registers are 8 bits or wider, space is wasted storing the Binary Coded Decimal numbers.
Packed Binary Coded Decimal
In packed Binary Coded Decimal numbers, a single 8-bit register stores two Binary Coded Decimal digits. By shifting the number in the upper register to the left four times and then adding the numbers in the upper and lower registers, the process combines two Binary Coded Decimal digits, which enables storage in a single register.
Binary Coded Decimal vs. Simple Binary representation
In simple binary representation, the whole number is converted into its binary form by dividing the number by 2 repeatedly. In binary-coded decimal, each individual digit is converted to binary. The 4-bit binary equivalent of each digit is then written together.
Example:
The decimal number is 234.
In binary format, 234 = 11101010
In binary-coded decimal, 234 is written as the following:
2 = 0010, 3 = 0011, 4 = 0100
Hence, 234 = 001000110100
Read About DSA Interview Questions.