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 – Create a File
Create a Custom Exception in Java
Java Program to Check whether Graph is a Bipartite using DFS
Java Program to Implement Stack API
Introduction to Spring Data REST
Java Program to Implement Jarvis Algorithm
Java Program to Perform Insertion in a 2 Dimension K-D Tree
Java Program to Find Median of Elements where Elements are Stored in 2 Different Arrays
Java Deep Learning Essentials - Yusuke Sugomori
Java Program to subtract two large numbers using Linked Lists
Java Program to add two large numbers using Linked List
Java Program to Implement Repeated Squaring Algorithm
Spring Boot with Multiple SQL Import Files
Exploring the New Spring Cloud Gateway
Java Program to Implement Stack
Spring Cloud AWS – RDS
Getting Started with Stream Processing with Spring Cloud Data Flow
REST Web service: HTTP Status Code và xử lý ngoại lệ RESTful web service với Jersey 2.x
Entity To DTO Conversion for a Spring REST API
Java Program to Implement RenderingHints API
Java program to Implement Tree Set
Check If a String Is Numeric in Java
Java Program to Implement Hash Tables with Quadratic Probing
New Features in Java 9
Extract network card address
Java InputStream to Byte Array and ByteBuffer
Java Program to Implement LinkedList API
Java Concurrency Interview Questions and Answers
Spring Cloud Series – The Gateway Pattern
LinkedList trong java
Java Program to Implement Graham Scan Algorithm to Find the Convex Hull
Spring Cloud – Bootstrapping