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:
Spring Boot - Zuul Proxy Server and Routing
An Intro to Spring Cloud Contract
Java Program to Implement Treap
Using JWT with Spring Security OAuth
Java Program to Check whether Graph is a Bipartite using 2 Color Algorithm
String Initialization in Java
HashSet trong Java hoạt động như thế nào?
Phân biệt JVM, JRE, JDK
Java Program to Implement Ternary Search Tree
Java Program to Implement Heap
Java Program to Implement Max-Flow Min-Cut Theorem
Java Program to Check if a Given Binary Tree is an AVL Tree or Not
Write/Read cookies using HTTP and Read a file from the internet
Java Program to Implement Binomial Tree
Introduction to Liquibase Rollback
A Guide to Iterator in Java
Spring Data – CrudRepository save() Method
OAuth 2.0 Resource Server With Spring Security 5
New Features in Java 10
Java Program to Check if a Matrix is Invertible
Java Program to Find Number of Spanning Trees in a Complete Bipartite Graph
How to Get All Dates Between Two Dates?
Java Program to Implement Sorted Circular Doubly Linked List
Dockerizing a Spring Boot Application
How to Return 404 with Spring WebFlux
Java List UnsupportedOperationException
A Guide to the Java LinkedList
Convert char to String in Java
Java Program to find the number of occurrences of a given number using Binary Search approach
Java Program to Implement Splay Tree
Custom Error Pages with Spring MVC
Logout in an OAuth Secured Application