This is the java program to perform addition of two numbers without using any arithmetic operators. The summation of two numbers can be obtained using XOR operation and carry can be obtained using AND performed at bit level.
Here is the source code of the Java Program to Perform Addition Operation Using Bit-wise Operators. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.
//This is sample program to perform addition operation using bitwise operators.
import java.util.Scanner;
public class Bitwise_Addition
{
static int add(int x, int y)
{
int carry;
while(y!=0)
{
carry = x & y;
x = x ^ y;
y = carry << 1;
}
return x;
}
public static void main(String args[])
{
Scanner input = new Scanner(System.in);
System.out.println("Enter the numbers to be added:");
int x = input.nextInt();
int y = input.nextInt();
System.out.println("The Summation is: "+add(x, y));
input.close();
}
}
Output:
$ javac Bitwise_Addition.java $ java Bitwise_Addition Enter the numbers to be added: 15 16 The Summation is: 31
Related posts:
Converting between an Array and a List in Java
Java Streams vs Vavr Streams
Generic Constructors in Java
Java Program to Implement ScapeGoat Tree
Java Program to Permute All Letters of an Input String
Tính kế thừa (Inheritance) trong java
XML Serialization and Deserialization with Jackson
Java Program to Implement Horner Algorithm
Working With Maps Using Streams
Spring Data – CrudRepository save() Method
Lớp lồng nhau trong java (Java inner class)
Java 8 Streams peek() API
Flattening Nested Collections in Java
Assertions in JUnit 4 and JUnit 5
Checking for Empty or Blank Strings in Java
Handling Errors in Spring WebFlux
Join and Split Arrays and Collections in Java
Java Program to Implement Control Table
Easy Ways to Write a Java InputStream to an OutputStream
HttpAsyncClient Tutorial
Toán tử trong java
Java – Generate Random String
Implementing a Binary Tree in Java
Send email with JavaMail
Truyền giá trị và tham chiếu trong java
Serve Static Resources with Spring
Phân biệt JVM, JRE, JDK
The XOR Operator in Java
Java Program to Implement Triply Linked List
How to Kill a Java Thread
Java Program to Solve Knapsack Problem Using Dynamic Programming
Java Program to Show the Duality Transformation of Line and Point