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 Strassen Algorithm
Java Program to Check if a Given Set of Three Points Lie on a Single Line or Not
Java Program to Find MST (Minimum Spanning Tree) using Prim’s Algorithm
Remove the First Element from a List
Java Program to Represent Graph Using 2D Arrays
JUnit 5 @Test Annotation
Java Program to Implement HashTable API
Remove All Occurrences of a Specific Value from a List
Primitive Type Streams in Java 8
Programmatic Transaction Management in Spring
Java Program to Generate Date Between Given Range
Java Program to Check Cycle in a Graph using Topological Sort
Hướng dẫn Java Design Pattern – Service Locator
Introduction to Java Serialization
Java NIO2 Path API
Java Program to Generate All Subsets of a Given Set in the Lexico Graphic Order
Java Program to Check if any Graph is Possible to be Constructed for a Given Degree Sequence
Automatic Property Expansion with Spring Boot
Spring REST API + OAuth2 + Angular
Java Program to Find SSSP (Single Source Shortest Path) in DAG (Directed Acyclic Graphs)
Java InputStream to String
Compact Strings in Java 9
Java Program to Check if it is a Sparse Matrix
Java CyclicBarrier vs CountDownLatch
MyBatis with Spring
Java Program to Implement Network Flow Problem
Java Program to Implement RoleList API
Spring Boot - Admin Server
Spring Boot: Customize the Jackson ObjectMapper
Java Program to Implement Find all Forward Edges in a Graph
Java 8 Stream findFirst() vs. findAny()
The HttpMediaTypeNotAcceptableException in Spring MVC