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:
Chương trình Java đầu tiên
Thao tác với tập tin và thư mục trong Java
Java Program to Sort an Array of 10 Elements Using Heap Sort Algorithm
A Guide to JUnit 5
Java Program to Check if it is a Sparse Matrix
Converting Java Date to OffsetDateTime
So sánh HashSet, LinkedHashSet và TreeSet trong Java
Serialization và Deserialization trong java
Spring Security 5 for Reactive Applications
Java Program to Implement Dijkstra’s Algorithm using Queue
Java – Get Random Item/Element From a List
Getting a File’s Mime Type in Java
Remove HTML tags from a file to extract only the TEXT
Java Program to Implement Queue using Linked List
Java 9 Stream API Improvements
Java Program to Implement Solovay Strassen Primality Test Algorithm
Reversing a Linked List in Java
Configure a Spring Boot Web Application
Java Program for Douglas-Peucker Algorithm Implementation
Guide to the Java Queue Interface
Hướng dẫn kết nối cơ sở dữ liệu với Java JDBC
Java Program to Apply Above-Below-on Test to Find the Position of a Point with respect to a Line
Queue và PriorityQueue trong Java
Java Program to Implement the Binary Counting Method to Generate Subsets of a Set
LinkedHashSet trong java
A Guide to LinkedHashMap in Java
Java Program to Implement Slicker Algorithm that avoids Triangulation to Find Area of a Polygon
REST Web service: HTTP Status Code và xử lý ngoại lệ RESTful web service với Jersey 2.x
Form Validation with AngularJS and Spring MVC
Using a Spring Cloud App Starter
Java Program to Implement Double Ended Queue
Spring Cloud – Securing Services