Binary to BCD and BCD to Binary

Introduction of Binary Coded Decimal conversions

45210

BCD to Binary

BCD numbers are representations of decimal (base 10) numbers, and like all modern number systems, BCD numbers use positional weighting. Each BCD digit in a given number contributes a magnitude equal to the digit multiplied by its weight, and each digit’s weight is equal to 10 raised to the power of the digit’s position in the number. Consider the BCD number 987, stored as three 4-bit BCD codes: 1001 for 9 (digit 2), 1000 for 8 (digit 1), and 0111 for 7 (digit 0). To find the binary equivalent, each BCD digit is multiplied by its weighted magnitude: 9 x 10^2 + 8 * 10^1 + 7 * 10^0, or 9 * 100 + 8 * 10+ 7 * 1. The Verilog code below illustrates converting a 4-digit BCD number to it’s binary equivalent.

module bcd2bin
   (
    input wire [3:0] bcd3, 
    input wire [3:0] bcd2, 
    input wire [3:0] bcd1, 
    input wire [3:0] bcd0, 
    output wire [13:0] bin
   );

   assign bin = (bcd3 * 10'd1000) + (bcd2*7'd100) + (bcd1*4'd10) + bcd0;

endmodule

Binary to BCD

The “double dabble” algorithm is commonly used to convert a binary number to BCD. The binary number is left-shifted once for each of its bits, with bits shifted out of the MSB of the binary number and into the LSB of the accumulating BCD number. After every shift, all BCD digits are examined, and 3 is added to any BCD digit that is currently 5 or greater.

This works because every left shift multiplies all BCD digits by two. Since BCD digits cannot exceed nine, a pre-shift number of five or more would result in a post-shift number of ten or more, which cannot be represented. Adding three to any BCD digit greater than five does two things: first, at the next shift, the 3 that was added becomes 6, and that accounts for the difference in binary and BCD codes (BCD uses 10 binary codes, and binary uses 16); and second, adding 3 forces the MSB of the BCD digit to a 1, where it is “carried out” and into the next digit. The figure below illustrates the process (the blue boxes around BCD digits show BCD digits that are >=5, and therefore need 3 to be added).

Figure 1. An illustration of the double-dabble algorithm
Figure 1. An illustration of the double-dabble algorithm

Verilog code to implement the double-dabble is shown below. Here, a for loop is used to make the code more compact. Note the for loop does not directly describe a circuit – rather, it describes how the circuit components that will do the required shifting and adding are to be assembled. The resulting circuit is a purely combinational circuit (the code could have been written linearly, without using a for loop, but it would have been much longer).

module bin2bcd(
   input [13:0] bin,
   output reg [15:0] bcd
   );
   
integer i;
	
always @(bin) begin
    bcd=0;		 	
    for (i=0;i<14;i=i+1) begin					//Iterate once for each bit in input number
        if (bcd[3:0] >= 5) bcd[3:0] = bcd[3:0] + 3;		//If any BCD digit is >= 5, add three
	if (bcd[7:4] >= 5) bcd[7:4] = bcd[7:4] + 3;
	if (bcd[11:8] >= 5) bcd[11:8] = bcd[11:8] + 3;
	if (bcd[15:12] >= 5) bcd[15:12] = bcd[15:12] + 3;
	bcd = {bcd[14:0],bin[13-i]};				//Shift one bit, and shift in proper bit from input 
    end
end
endmodule