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 Brent Cycle Algorithm
A Quick Guide to Using Keycloak with Spring Boot
Java Program to Perform the Sorting Using Counting Sort
Overflow and Underflow in Java
Java Program to Implement LinkedTransferQueue API
A Guide to @RepeatedTest in Junit 5
Java Program to Implement Shoelace Algorithm
A Guide to LinkedHashMap in Java
Spring Boot Gradle Plugin
Java Collections Interview Questions
Hướng dẫn sử dụng Java Annotation
Java Program to Repeatedly Search the Same Text (such as Bible by building a Data Structure)
Receive email by java client
Redirect to Different Pages after Login with Spring Security
Integer Constant Pool trong Java
Life Cycle of a Thread in Java
Hướng dẫn Java Design Pattern – Composite
Java Program to Find MST (Minimum Spanning Tree) using Prim’s Algorithm
wait() and notify() Methods in Java
Write/Read cookies using HTTP and Read a file from the internet
Jackson – JsonMappingException (No serializer found for class)
LinkedList trong java
Java Program to Implement Binomial Tree
Java Program to find the maximum subarray sum O(n^2) time(naive method)
Spring AMQP in Reactive Applications
Một số tính năng mới về xử lý ngoại lệ trong Java 7
Java Program to Implement Trie
Java Program to Implement Interpolation Search Algorithm
Java Program to Find the Minimum value of Binary Search Tree
Java Program to Implement Min Heap
Concatenating Strings In Java
Java Switch Statement