2 Ways to Add Binary Numbers in Java - Coding Example (original) (raw)

In the last article, I have shown you how to subtract binary numbers in Java, and today, you will learn the opposite i.e. how to add binary numbers in Java. You can do binary addition in Java either by writing your own logic or by taking advantage of the Java API, which allows you to convert a binary string into a binary number. Though, if you want you can also use the logic I have shared in my earlier post about how to check binary numbers to verify input before converting it to a binary number. In this article, we'll take a look at both methods of adding two binary numbers in Java. In this article, I have given you two solutions to add binary numbers in Java.

In the first solution, we have used the Java API, which first converts the given binary String to a decimal number using the Integer.parseInt() method, which is also used to convert String to Integer.

This method is overloaded to convert String to an integer in several other number systems e.b. binary, octal, decimal, and hexadecimal.

The overloaded version takes another int parameter radix, which you can use to convert a binary String to a decimal integer like Integer.parseInt(number, 2), where the number is a String denoting a binary number. For example, String "101" will be converted into integer 5 and "1000" will be converted into 8.

Once you have got the number in decimal format, you can just add those numbers and convert the result to the binary String by using Integer.toBinaryString(sum);.

That's all is needed if you use Java API as the first convert binary String to decimal numbers, add them and then convert the result back to binary form.

The second solution is a bit complex because we are actually doing the binary addition with binary numbers i.e. we are adding numbers from the right and then carrying the carry towards the left. It's how you do in the paper. You can further check these free data structure and algorithms courses to learn more bout coding essentials like data structure, binary system, etc.

Java Program to add two binary numbers? Examples

Here is our complete solution of adding two binary numbers in Java. You can run this Java program into your favorite Java IDE like Eclipse or NetBeans or IntelliJIDEA, or even from a command prompt to add two given binary numbers.

import java.util.Scanner;

/*

public class Main {

public static void main(String[] args) {

System.out.println("Welcome to Java program to add two binary numbers");
Scanner scnr = new Scanner(System.in);

System.out.println("Please enter first binary number");
String first = scnr.nextLine();

System.out.println("Please enter second binary number");
String second = scnr.nextLine();

String addition = add(first, second);
System.out.println("addition of two binary number is : " + addition);

String sum = sum(first, second);
System.out.println("Sum of two binary number is : " + sum);

scnr.close();

}

/**

/**

}
return (carry == 0) ? res : "1" + res;

}

}

Output Welcome to Java program to add two binary numbers Please enter first binary number 1010 Please enter second binary number 11 addition of two binary number is : 1101 Sum of two binary number is : 1101

If you are not sure how this program works, consider debugging this Java program by the instructions given in this article. When you debug the code inside the first method you can clearly see that Integer. parseInt(number, 2) converts a binary number into a decimal integer.

Coding - 2 Ways to Add Binary Numbers in Java

That's all about how to write a Java program to add two binary numbers. You can use any of the methods to perform binary addition, but if you are asked in interviews, you should first use the Java way by using Integer.toString() method and then write your logic to calculate the sum of two binary numbers, if and only if Interviewer asked you to do so.

Further Learning
How to do the binary search in Java?
10 Algorithm books Every Programmer Should Read
How to implement Quicksort algorithm in Java?
5 Free Courses to learn Data Structure and Algorithms

Thanks for reading this article so far. If you like this article, then please share it with your friends and colleagues. If you have any questions or feedback then please drop a note.