Binary Coded Decimal (BCD) Addition

Learn via video course
FREE
View all courses
DSA Problem Solving for Interviews using Java
DSA Problem Solving for Interviews using Java
by Jitender Punia
1000
4.9
Start Learning
DSA Problem Solving for Interviews using Java
DSA Problem Solving for Interviews using Java
by Jitender Punia
1000
4.9
Start Learning
Topics Covered

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.

Build an AI-First Career, Master the Complete Skillset

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program
NSDC Certified

AI Forward Deployed Engineer Program

Full-stack engineering, production AI and client-facing consulting

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program

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.

Sharpen Your Fundamentals with Free Learning

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<=21051 <= \text{length of string} <= 2 * 10^5

How Scaler Transformed Careers in Different Fields

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

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

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

C++ implementation

Java implementation

Output:

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))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)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.