Java Program to add two binary numbers
Program
import java.util.Scanner;
public class AddTwoBinaryNumbers {
public void addBinaryNumbers(long binaryNum1, long binaryNum2)
{
int i = 0;
int carry = 0;
int[] sum = new int[20];
while (binaryNum1 != 0 || binaryNum2 != 0)
{
sum[i++] = (int)((binaryNum1 % 10 + binaryNum2 % 10 + carry) % 2);
carry = (int)((binaryNum1 % 10 + binaryNum2 % 10 + carry) / 2);
binaryNum1 = binaryNum1 / 10;
binaryNum2 = binaryNum2 / 10;
}
if (carry != 0) {
sum[i++] = carry;
}
--i;
while (i >= 0) {
System.out.print(sum[i--]);
}
}
public static void main(String[] args)
{
AddTwoBinaryNumbers addTwoBinaryNumbers = new AddTwoBinaryNumbers();
Scanner reader = new Scanner(System.in);
System.out.print("Enter the first binary number: ");
long binaryNum1 = reader.nextLong();
System.out.print("Enter the second binary number: ");
long binaryNum21 = reader.nextLong();
System.out.println("Sum of two binary numbers is: ");
addTwoBinaryNumbers.addBinaryNumbers(binaryNum1,binaryNum21);
}
}
This program demonstrates adding two binary numbers without converting them into decimal.
Use Case:
This program is useful for understanding binary arithmetic and is suitable for scenarios where direct binary manipulation is required, such as in low-level programming or hardware design simulations.
-
Input:
- The user is prompted to enter two binary numbers as
long
integers using theScanner
class.
- The user is prompted to enter two binary numbers as
-
Binary Addition:
- Logic:
- Binary addition follows these rules:
- 0 + 0 = 0
- 0 + 1 = 1
- 1 + 1 = 10 (sum = 0, carry = 1)
- 1 + 1 + carry = 11 (sum = 1, carry = 1)
- Binary addition follows these rules:
- The program performs digit-by-digit addition from right to left (least significant digit to most significant digit).
- Each digit from both binary numbers is added along with any carry from the previous operation.
- Logic:
-
Implementation:
- Variables:
i
: Index for storing digits in thesum
array.carry
: Tracks carry-over during addition.sum[]
: Array to store the result of binary addition in reverse order.
- While Loop:
- The loop runs until both binary numbers become
0
. % 10
: Extracts the last digit of each binary number./ 10
: Removes the last digit of the binary number.(binaryNum1 % 10 + binaryNum2 % 10 + carry) % 2
: Computes the binary sum for the current position.(binaryNum1 % 10 + binaryNum2 % 10 + carry) / 2
: Updates the carry for the next iteration.
- The loop runs until both binary numbers become
- Handling Remaining Carry:
- If there is a carry left after the loop, it is added to the
sum
array.
- If there is a carry left after the loop, it is added to the
- Variables:
-
Output:
- The
sum
array is printed in reverse order to display the final binary sum.
- The
-
Execution:
- Example:
Enter the first binary number: 1101 Enter the second binary number: 1011 Sum of two binary numbers is: 11000
- Steps:
- Add 1 + 1 = 10 (sum = 0, carry = 1)
- Add 0 + 1 + carry = 10 (sum = 0, carry = 1)
- Add 1 + 0 + carry = 10 (sum = 0, carry = 1)
- Add 1 + carry = 10 (sum = 1, carry = 0)
- Final result:
11000
.
- Example:
Key Features:
- Binary Arithmetic:
- The program handles binary addition without converting to decimal, preserving efficiency for binary computations.
- Storage of Result:
- The
sum
array stores digits in reverse order and is printed backward to ensure the correct order.
- The
Output
$ javac AddTwoBinaryNumbers.java
$ java AddTwoBinaryNumbers
Enter the first binary number: 100101
Enter the second binary number: 101101
Sum of two binary numbers is:
1010010