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:
Assert an Exception is Thrown in JUnit 4 and 5
Java Program to Implement ConcurrentHashMap API
Properties with Spring and Spring Boot
Java Program to Implement Circular Doubly Linked List
How to Count Duplicate Elements in Arraylist
Introduction to Apache Commons Text
A Guide to Java HashMap
Extra Login Fields with Spring Security
Tìm hiểu cơ chế Lazy Evaluation của Stream trong Java 8
Spring Boot - Apache Kafka
Java Program to Find the Minimum Element of a Rotated Sorted Array using Binary Search approach
Java Program to Implement Merge Sort Algorithm on Linked List
Spring MVC Async vs Spring WebFlux
Filtering and Transforming Collections in Guava
Configure a RestTemplate with RestTemplateBuilder
A Guide to EnumMap
Spring AMQP in Reactive Applications
Java Program to Implement Cubic convergence 1/pi Algorithm
How to Change the Default Port in Spring Boot
Tiêu chuẩn coding trong Java (Coding Standards)
So sánh HashMap và HashSet trong Java
Java Program to Implement Segment Tree
Hướng dẫn Java Design Pattern – State
Java Program to Compute DFT Coefficients Directly
Apache Commons Collections MapUtils
Java Program to Find MST (Minimum Spanning Tree) using Prim’s Algorithm
Java Program to Implement Min Heap
Remove HTML tags from a file to extract only the TEXT
Java InputStream to String
Java Program to Implement Hash Tree
Spring’s RequestBody and ResponseBody Annotations
Java Program to Implement Treap