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 Red Black Tree
Jackson – Decide What Fields Get Serialized/Deserialized
Injecting Prototype Beans into a Singleton Instance in Spring
An Intro to Spring Cloud Security
Java Program to Perform Postorder Recursive Traversal of a Given Binary Tree
Java Program to Solve Knapsack Problem Using Dynamic Programming
Send email with authentication
Java Program to Implement Miller Rabin Primality Test Algorithm
Instance Profile Credentials using Spring Cloud
Intro to Spring Boot Starters
Java Program to Implement Quick Hull Algorithm to Find Convex Hull
Quick Guide to the Java StringTokenizer
Java Program to Implement Borwein Algorithm
Remove the First Element from a List
An Intro to Spring Cloud Zookeeper
Hashtable trong java
Spring RestTemplate Request/Response Logging
Spring Autowiring of Generic Types
Hướng dẫn sử dụng Java Reflection
Java Program to find the maximum subarray sum O(n^2) time(naive method)
Why String is Immutable in Java?
Generate Spring Boot REST Client with Swagger
Using the Map.Entry Java Class
Java Program to Implement Booth Algorithm
Rest Web service: Filter và Interceptor với Jersey 2.x (P1)
Sử dụng Fork/Join Framework với ForkJoinPool trong Java
Find the Registered Spring Security Filters
Java Program to Implement Queue
Java Program to Implement Brent Cycle Algorithm
Converting between an Array and a List in Java
How to Find an Element in a List with Java
Java Program to Describe the Representation of Graph using Adjacency List