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 Security Form Login
XML Serialization and Deserialization with Jackson
Spring Boot - Actuator
Java toString() Method
Java Program to Test Using DFS Whether a Directed Graph is Weakly Connected or Not
Migrating from JUnit 4 to JUnit 5
Guide to DelayQueue
Java Program to Find Strongly Connected Components in Graphs
Allow user:password in URL
Java Program to Find Location of a Point Placed in Three Dimensions Using K-D Trees
Custom Exception trong Java
Fixing 401s with CORS Preflights and Spring Security
Java 8 StringJoiner
Java Program to Generate a Random Subset by Coin Flipping
Java Program to Find Median of Elements where Elements are Stored in 2 Different Arrays
Constructor Injection in Spring with Lombok
Tính đa hình (Polymorphism) trong Java
Extract network card address
Spring MVC Tutorial
Automatic Property Expansion with Spring Boot
Java Program to Implement Min Heap
Java Program to Implement Efficient O(log n) Fibonacci generator
Tips for dealing with HTTP-related problems
Handle EML file with JavaMail
Java Program to Implement Gale Shapley Algorithm
Java Program to Find Nearest Neighbor for Static Data Set
String Initialization in Java
Java Program to Implement Bellman-Ford Algorithm
Java Program to Implement Multi-Threaded Version of Binary Search Tree
Java Program to Implement the Program Used in grep/egrep/fgrep
Java Program to Implement Uniform-Cost Search
Spring Cloud Bus