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:
MyBatis with Spring
RestTemplate Post Request with JSON
An Introduction to Java.util.Hashtable Class
Java Program to Implement ArrayDeque API
Primitive Type Streams in Java 8
Spring Boot - Enabling Swagger2
Guide To CompletableFuture
Spring Boot - Internationalization
Convert char to String in Java
Spring Boot - Web Socket
Java Program to Use rand and srand Functions
Collection trong java
Spring Boot - Rest Controller Unit Test
Java 8 Predicate Chain
Spring Boot - Hystrix
RegEx for matching Date Pattern in Java
Guide to PriorityBlockingQueue in Java
Java Program to Implement Maximum Length Chain of Pairs
Java Program to Implement Self Balancing Binary Search Tree
Introduction to Project Reactor Bus
SOAP Web service: Upload và Download file sử dụng MTOM trong JAX-WS
Unsatisfied Dependency in Spring
Spring Web Annotations
Java Program to Implement Sieve Of Eratosthenes
Java Program to Create a Balanced Binary Tree of the Incoming Data
Allow user:password in URL
Java Program to Search for an Element in a Binary Search Tree
Sorting Query Results with Spring Data
Guide to Apache Commons CircularFifoQueue
Comparing Objects in Java
How to Count Duplicate Elements in Arraylist
A Guide to TreeMap in Java