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 Perform Deletion in a BST
Introduction to Eclipse Collections
Guide to the Volatile Keyword in Java
Converting Between an Array and a Set in Java
Java Program to Implement Naor-Reingold Pseudo Random Function
Java Program to Implement Extended Euclid Algorithm
Java Program to Implement Hash Tables with Quadratic Probing
Java Program to Implement Fisher-Yates Algorithm for Array Shuffling
Using Java Assertions
Spring Security – security none, filters none, access permitAll
Array to String Conversions
Java Multi-line String
How to use the Spring FactoryBean?
Validate email address exists or not by Java Code
Java – File to Reader
Spring Boot - Unit Test Cases
Spring Cloud Connectors and Heroku
Java Program to Implement Knapsack Algorithm
StringBuilder vs StringBuffer in Java
Injecting Prototype Beans into a Singleton Instance in Spring
Life Cycle of a Thread in Java
Remove the First Element from a List
Spring Data JPA @Query
Một số nguyên tắc, định luật trong lập trình
Spring Security with Maven
Java Program to Implement Dijkstra’s Algorithm using Queue
Debug a HttpURLConnection problem
Deploy a Spring Boot App to Azure
Java Program to Implement LinkedTransferQueue API
Java Program to Implement VList
How to Break from Java Stream forEach
Introduction to Spring Cloud Stream