
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Java Program to Add Two Binary Strings
In this article, we will understand how to add two binary strings in Java. A binary string is a sequence of numbers represented in bytes 0s and 1s.
Below is a demonstration of the same −
Input
Suppose our input is −
10101 10001
Output
The desired output would be −
100110
Algorithm
Step 1- START Step 2- Create new scanner object Step 3- Enter two binary inputs Step 4- Define a carry flag Step 5- Use while condition to check if they are equal to 0 Step 6- If not, use the % operator and the carry flag to perform bitwise addition Step 7-Display it as result Step 8-STOP
Example 1
Here, the input is being entered by the user based on a prompt. You can try this example live in ourcoding ground tool .
import java.util.*; public class AddBinaryNumbers { public static void main(String[] args) { long binary_input_1 , binary_input_2 ; System.out.println("Required packages have been imported"); Scanner input = new Scanner(System.in); System.out.println("A reader object has been defined "); System.out.print("Enter the first binary number : "); binary_input_1 = input.nextLong(); System.out.print("Enter the second binary number : "); binary_input_2 = input.nextLong(); int i, carry ; i = 0; carry = 0; int[] binary_sum = new int[10]; while (binary_input_1 != 0 || binary_input_2 != 0) { binary_sum[i++] = (int) (carry + (binary_input_1 % 10 + binary_input_2 % 10) % 2); carry = (int) ((binary_input_1 % 10 + binary_input_2 % 10 + carry) / 2); binary_input_1 = binary_input_1 / 10; binary_input_2 = binary_input_2 / 10; } if (carry != 0) { binary_sum[i++] = carry; } --i; System.out.print("\nThe sum of the binary numbers is: "); while (i >= 0) { System.out.print(binary_sum[i--]); } System.out.print("\n"); } }
Output
Required packages have been imported A reader object has been defined The first binary number is 10101 The second binary number is 10001 The sum of the binary is: 100110
Example 2
Here, the integer has been previously defined, and its value is accessed and displayed on the console.
public class AddingBinaryNumbers { public static void main(String[] args) { long binary_input_1 , binary_input_2 ; binary_input_1 = 10101; binary_input_2 = 10001; System.out.print("The first binary number is " + binary_input_1); System.out.print("\nThe second binary number is " + binary_input_2); int i, carry ; i = 0; carry = 0; int[] binary_sum = new int[10]; while (binary_input_1 != 0 || binary_input_2 != 0) { binary_sum[i++] = (int) (carry + (binary_input_1 % 10 + binary_input_2 % 10) % 2); carry = (int) ((binary_input_1 % 10 + binary_input_2 % 10 + carry) / 2); binary_input_1 = binary_input_1 / 10; binary_input_2 = binary_input_2 / 10; } if (carry != 0) { binary_sum[i++] = carry; } --i; System.out.print("\nThe sum of the binary numbers is: "); while (i >= 0) { System.out.print(binary_sum[i--]); } System.out.print("\n"); } }
Output
The first binary number is 10101 The second binary number is 10001 The sum of the binary numbers is: 100110
- Related Articles
- Program to add two binary strings in C++
- Haskell program to add binary strings
- Program to add two binary strings, and return also as binary string in C++
- How to add Two Binary Strings in Golang?
- Add n binary strings?
- Java Program to Compare Two Strings
- Program to Compare two strings in Java
- Java Program to Compare two strings lexicographically
- Add n binary strings in C++?
- Program to add two numbers represented as strings in Python
- Java program to add two integers
- Java program to add two matrices.
- Java Program to Add Two Numbers
- Java Program to Add the two Numbers
- Java Program to Add Two Complex numbers

Advertisements