## DEV Community

JavaFullStackDev.in

Posted on • Updated on

# Binary Addition in Java: A Comprehensive Guide

Binary addition is a fundamental operation in computer science and digital electronics. It operates similarly to decimal addition but uses only two digits: 0 and 1. Understanding binary addition is crucial for various applications, including low-level programming, digital circuit design, and data representation. In this article, we will explore how to perform binary addition in Java, providing a thorough explanation along with sample code.

• 0 + 0 = 0
• 0 + 1 = 1
• 1 + 0 = 1
• 1 + 1 = 0 (carry 1 to the next higher bit)

When performing binary addition, if the sum of two bits exceeds 1, the excess is carried over to the next higher bit. For instance:

1101
+ 1011
------
11000

Here, the addition of each bit from right to left results in a carry that is added to the next higher bit.

#### Steps for Binary Addition in Java

1. Input Binary Numbers: Read or receive binary numbers as input strings.
2. Equalize Lengths: Ensure both binary strings are of equal length by padding the shorter one with leading zeros.
3. Add Bit by Bit: Perform addition from the least significant bit (rightmost) to the most significant bit (leftmost), keeping track of carry.
4. Handle Final Carry: If there is a carry left after the most significant bit, prepend it to the result.

#### Java Implementation

Below is a detailed Java implementation for binary addition:

public static void main(String[] args) {
String binary1 = "1101";
String binary2 = "1011";

System.out.println("Binary Addition of " + binary1 + " and " + binary2 + " is: " + result);
}

public static String addBinary(String binary1, String binary2) {
// Ensure both binary strings are of the same length
int length = Math.max(binary1.length(), binary2.length());

StringBuilder result = new StringBuilder();
int carry = 0;

// Perform bit-by-bit addition from right to left
for (int i = length - 1; i >= 0; i--) {
int bit1 = binary1.charAt(i) - '0'; // Convert char to int
int bit2 = binary2.charAt(i) - '0';

int sum = bit1 + bit2 + carry;
carry = sum / 2; // Calculate new carry
result.append(sum % 2); // Append result bit
}

// If there's a carry left, append it
if (carry != 0) {
result.append(carry);
}

// Reverse the result to get the correct binary sum
return result.reverse().toString();
}

public static String padLeftZeros(String binary, int length) {
StringBuilder sb = new StringBuilder();
while (sb.length() < length - binary.length()) {
sb.append('0');
}
sb.append(binary);
return sb.toString();
}
}

#### Explanation of the Code

1. Main Method:

• Initializes two binary strings, binary1 and binary2.
• Prints the result.

• Pads the binary strings to make them of equal length using the padLeftZeros method.
• Initializes a StringBuilder to build the result and a variable carry to keep track of the carry.
• Iterates over the binary strings from right to left, converting each character to an integer and performing the addition.
• Calculates the new carry and appends the result bit.
• Appends the final carry if it exists and reverses the result to get the correct binary sum.