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:
Java Program to Implement Skip List
Java Program to do a Breadth First Search/Traversal on a graph non-recursively
Getting Started with Stream Processing with Spring Cloud Data Flow
Java Program to Implement Quick Sort Using Randomization
Java Program to Check Whether it is Weakly Connected or Strongly Connected for a Directed Graph
Debug a JavaMail Program
Java Program to Check whether Graph is Biconnected
Java Program to Implement Sorting of Less than 100 Numbers in O(n) Complexity
Assert an Exception is Thrown in JUnit 4 and 5
Java Program to Implement Circular Doubly Linked List
Java Program to Represent Graph Using Adjacency Matrix
Service Registration with Eureka
Guide to CountDownLatch in Java
Java Program to Find Shortest Path Between All Vertices Using Floyd-Warshall’s Algorithm
Hướng dẫn sử dụng Java Annotation
Java Program to Generate a Graph for a Given Fixed Degree Sequence
Spring MVC Custom Validation
Annotation trong Java 8
Properties with Spring and Spring Boot
Upload and Display Excel Files with Spring MVC
Java Program to find the number of occurrences of a given number using Binary Search approach
Java Program to Implement the Checksum Method for Small String Messages and Detect
Removing all duplicates from a List in Java
Calling Stored Procedures from Spring Data JPA Repositories
Control Structures in Java
Java Program to Implement Strassen Algorithm
Guide to Guava Table
Java Program to Implement the Hungarian Algorithm for Bipartite Matching
Retrieve User Information in Spring Security
Java Program to Find Strongly Connected Components in Graphs
Apache Commons Collections BidiMap
Java Program to Check whether Graph is a Bipartite using BFS