Table of contents
1.
Introduction
2.
What is Binary Coded Decimal (BCD) Addition?
3.
Approach for BCD Addition
4.
Code Implementation
4.1.
C++
4.2.
Java
4.3.
Python
4.4.
C#
4.4.1.
Time and Space Complexity
5.
Frequently Asked Questions
5.1.
What is the purpose of BCD addition? 
5.2.
What are the rules for BCD addition?
5.3.
Why do we add 6 to BCD addition?
5.4.
What is 3 in BCD?
6.
Conclusion
Last Updated: Jan 7, 2025
Medium

Binary Coded Decimal (BCD) Addition

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Binary Coded Decimal (BCD) addition is an important method used in digital systems to ensure accurate decimal arithmetic. BCD represents each decimal digit in its binary form, allowing for precise operations in systems like calculators and digital clocks. In BCD addition, the process involves adding binary representations of decimal digits, with a special correction applied if the sum exceeds 9. This adjustment ensures the result remains a valid decimal value.

bcd addition

We know that every number can be represented uniquely in binary format. To be precise, we can represent all numbers up to 1e9 as a unique representation in 32 bits. It leads to another ecosystem of number representation that supports all mathematical operations. They are extremely interesting to work on. One such addition method is BCD which stands for Binary Coded Decimal addition.

You can also read about - Shift Registers in Digital Electronics and Difference Between Jfet and Mosfet

What is Binary Coded Decimal (BCD) Addition?

Binary Coded Decimal Addition is a well-defined way to add two decimal numbers represented in their corresponding binary format. On a simpler note, BCD addition refers to concatenating the binary representation of each digit obtained after the decimal addition of two numbers.

For example: Consider the numbers A = 20 and B = 40. The summation of A and B returns 60. The digits 60 are 6 and 0. So all we need to do is add the binary representation of 6 and 0. So we need to add the strings 0110(6) and 0000(0). Hence 01100000 is the desired output.

This method is used in electronic circuits requiring decimal arithmetic, such as calculators, digital clocks, and industrial control systems.

Approach for BCD Addition

After understanding the problem statement, we may adopt various possible solutions to code the BCD approach. 
 

  • One possible solution is using a hashmap to store the string representation of all digits.
     
  • We add the input numbers and store their digits in a vector. We reverse the vector to obtain the correct order for BCD addition. 
     
  • Finally, we replace the digits in the vector with their string representations and obtain the required answer.
     
  • We may also replace the hashmap with a simple array, as represented in the Java` & python code below.

Also see,  what is middleware

Code Implementation

We will see the implementation of BCD addition in various languages.

The implementation of BCD addition in C++ , Python , Java, C#, Javascript goes as follows:

  • C++
  • Java
  • Python
  • C#

C++

#include <bits/stdc++.h>
using namespace std;
string BCD(int x,int y)
{
   int num = x+y;
   vector<int> vec;
   map<int,string>mpp;
   mpp[0]="0000";
   mpp[1]="0001";
   mpp[2]="0010";
   mpp[3]="0011";
   mpp[4]="0100";
   mpp[5]="0101";
   mpp[6]="0110";
   mpp[7]="0111";
   mpp[8]="1000";
   mpp[9]="1001";
   string ans="";
   while(num>0)
   {
       int d=num%10;
       vec.push_back(d);
       num/=10;
   }
   reverse(vec.begin(),vec.end());
   for(auto x:vec)
   {
       ans+=mpp[x];
   }
   return ans;
}
// Driver Code
int main()
{
int num1=10;
int num2=20;
cout << BCD(num1,num2);
return 0;
}
You can also try this code with Online C++ Compiler
Run Code

Java

public class Main
{
public static String BCD(int num1, int num2)
{
// Calculate sum
int p = num1 + num2 ;
String sumString = String.valueOf(p);
int n = sumString.length();
// To store the final result
String ans = "";
// Array containing all the binary values
String vec[] = { "0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001" };
for (int i = 0; i < n; i++)
{
String mx = vec[sumString.charAt(i) - '0'];
ans += mx;
}
// Returning ans
return ans;
}

public static void main(String[] args)
{
int a=10;
int b=20;
System.out.println(BCD(a,b));
}
}
You can also try this code with Online Java Compiler
Run Code

Python

def bcd(n1, n2):
p = n1 + n2
s = str(p)
n = len(s)
ans = ""
mx = ""
t = ["0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001"]
for i in range(len(s)):
mx = t[int(s[i])]
ans+=mx


return ans


a = 20
b = 30
print(bcd(a, b))
You can also try this code with Online Python Compiler
Run Code

C#

using System;

public class MainClass
{
public static string BCD(int num1, int num2)
{
// Calculate sum
int p = num1 + num2;
string sumString = p.ToString();
int n = sumString.Length;

// To store the final result
string ans = "";

// Array containing all the binary values
string[] vec = { "0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001" };

for (int i = 0; i < n; i++)
{
string mx = vec[sumString[i] - '0'];
ans += mx;
}

// Returning ans
return ans;
}

public static void Main(string[] args)
{
int a = 10;
int b = 20;
Console.WriteLine(BCD(a, b));
}
}


Output

00110000

The code explanation goes as follows:

  • The above code calls the function BCD addition for the designed task. 
     
  • In this function, we precompute and store the binary string representation of all digits in a hashmap. 
     
  • Now, we store all digits of the number in a vector. 
     
  • We reverse the vector to fix the order as digits were earlier generated in the opposite order. 
     
  • In the last step, we concatenate the strings in order and return the final string.


Explanation

In the above codes, we call the BCD function with 2 numbers to be added as the parameter.

The BCD function adds the two numbers and performs string concatenation of the BCD representation of the digits of the summation. These binary representations are stored in the string vector. The final string ans contains the resultant output.

Time and Space Complexity

It is important to know the time and space complexity of the code we have discussed above. Below is the calculated time and space complexity for BCD addition program.

Time Complexity: O(len(num1+num2)). The time complexity of the above code is O(len(num1+num2)) because, in the worst case, the summation of 2 numbers can be several lengths equal to the sum of lengths of the 2 numbers. Hence the final length of the string is equal to 4*(length of summation) because the size of each string in the hashmap is 4.

Space Complexity: O(1). The space complexity is O(1) because we are not using any major container to store data in the above codes. The hashmap always contains only ten elements; hence it will be considered constant space.

Frequently Asked Questions

What is the purpose of BCD addition? 

In digital circuits and microcontrollers, BCD addition is commonly used to perform arithmetic operations with decimal numbers. 

What are the rules for BCD addition?

The rules for BCD addition are as follows: Perform binary addition on each pair of BCD digits. If the result is greater than 9, add 6 to the result to correct the BCD representation. Carry-over digits are handled as usual.

Why do we add 6 to BCD addition?

In BCD addition, if the sum of two digits exceeds 9, we add 6 to correct the result. This adjustment helps convert the binary sum back to valid BCD format, ensuring each decimal digit stays within the 0-9 range.

What is 3 in BCD?

In Binary Coded Decimal (BCD), the decimal number 3 is represented as 0011. Each decimal digit in BCD is encoded as a separate 4-bit binary number, so 3 is written in its 4-bit binary equivalent, 0011, in BCD format.

Conclusion

The article was clear enough to explain the working and implementation of BCD addition in detail. This is an important algorithm often used in calculators and digital watches.

Demultiplexer

Binary to Octal conversion

Binary Numbers of N digits

All Binary Numbers in range from L to R

Live masterclass