BCD addition transforms the simple sum of two numbers, A and B, into a binary-coded marvel, concatenating each digit’s binary form from their sum into a precise, digital-friendly format.
What is BCD?
BCD, or Binary Coded Decimal, is a unique encoding system where every decimal digit is represented by its 4-bit binary counterpart. This method directly maps decimal digits 0 through 9 to binary, ensuring an easy-to-understand correspondence between the two systems. It excludes binary codes for values 10 to 15, which don’t represent valid decimal digits.
BCD Addition
BCD Addition involves a specialized process tailored for Binary Coded Decimal systems, ensuring decimal digits are correctly represented in binary form.
Steps of BCD Addition
Step 1: Start by adding the BCD numbers just like binary numbers.
Step 2: Check the 4-bit result. If it’s 9 or below, it’s already a valid BCD.
Step 3: For sums over 9 or with a carry-out, they’re not valid BCDs. Correct these by adding 6 (0110 in binary).
Step 4: If adding 6 generates a carry, move it to the next 4-bit group. This step keeps the result within BCD’s valid range, avoiding non-decimal codes.
Problem Statement
Given two numbers, A and B, the task here is to perform BCD addition of those two numbers. BCD addition of two numbers means the concatenation of the binary Representation of each digit retrieved by the sum of those two numbers.
BCD Addition Examples
Input: A = 20, B=30 Output: 1010000
Explanation: Sum of A and B = 50 Binary Representation of 5 = 0101 Binary Representation of 0 = 0000 Hence, BCD Addition is 0101+0000 = 1010000
Input: A = 5, B=13 Output: 11000
Explanation: Sum of A and B = 18 Binary Representation of 1 = 0001 Binary Representation of 8 = 1000 Hence, BCD Addition is 0001+1000 = 11000
Constraints
1<=length of string<=2∗105
Approach: Converting the summation of given two numbers to BCD Number
In this approach, we will be following the below algorithm:
- Find the sum of number A & B and store it in a variable.
- For each digit of the calculated sum, calculate its binary representation upto 4 Bits.
- Finally concatenate all the bits of the digits into the result variable and print the output.
Python Implementation
# Function for BCD Addition
def calculateBCD(A, B):
# calculate sum
sumString = str(A + B)
n = len(sumString)
# Array containing all the binary values
binary = [ "0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001" ]
ans = ""
# Concatenating the binary values
for i in range(n):
bin = binary[ord(sumString[i]) - ord('0')]
ans += str(bin)
# Returning ans
return int(ans)
# Given Numbers
A = 20
B = 30
print(calculateBCD(A, B))
C++ implementation
#include <bits/stdc++.h>
using namespace std;
// Function for BCD Addition
int calculateBCD(int A, int B)
{
// Calculate sum
string sumString = to_string(A + B);
int n = sumString.length();
// To store the final result
string ans;
// Array containing all the binary values
string binary[10] = { "0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001" };
// Concatenating the binary values
for (int i = 0; i < n; i++) {
string bin = binary[sumString[i] - '0'];
ans += bin;
}
// Returning ans
return stoi(ans);
}
// Driver Code
int main()
{
// Given Numbers
int A = 20, B = 30;
// Function Call
cout << calculateBCD(A, B);
return 0;
}
Java implementation
class Code{
// Function for BCD Addition
static int calculateBCD(int A, int B){
// Calculate sum
String sumString = String.valueOf(A + B);
int n = sumString.length();
// Array containing all the binary values
String binary[] = { "0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001" };
String ans = "";
// Concatenating the binary values
for(int i = 0; i < n; i++){
String bin = binary[sumString.charAt(i) - '0'];
ans += String.valueOf(bin);
}
// Returning ans
return Integer.parseInt(ans);
}
}
public class Main{
public static void main(String[] args){
// Given Numbers
int A = 20;
int B = 30;
System.out.println(Code.calculateBCD(A, B));
}
}
Output:
1010000
Complexity Analysis
Time Complexity: O(len(A+B)) => As a single loop is required to traverse all the digits of the sum of both the numbers and then calculate the binary representation. Hence the time complexity is O(len(A+B)).
Space Complexity: O(1) => Constant space is required to store the binary representation of the digits from 0 to 9. Hence the time complexity is O(1).
Conclusion
- BCD addition intricately converts the sum of decimal numbers into a binary format, ensuring each digit’s binary Representation is concatenated to form the final output.
- The process includes a straightforward binary addition followed by a validation step to ensure the sum remains within the valid BCD range of 0 to 9 for each digit.
- For sums exceeding 9 or producing a carry, a corrective step of adding 6 (0110 in binary) realigns the result within BCD’s constraints, bypassing invalid codes.
- This method is essential for digital systems requiring precise decimal-to-binary conversion, highlighting its significance in computing and electronic devices.