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:
Debug a HttpURLConnection problem
Daemon Threads in Java
A Guide to the ViewResolver in Spring MVC
Guide to the Java TransferQueue
Giới thiệu java.io.tmpdir
Show Hibernate/JPA SQL Statements from Spring Boot
Documenting a Spring REST API Using OpenAPI 3.0
Convert String to Byte Array and Reverse in Java
Guide to the Volatile Keyword in Java
Getting Started with Stream Processing with Spring Cloud Data Flow
Java Program to Check Whether Graph is DAG
OAuth 2.0 Resource Server With Spring Security 5
Logout in an OAuth Secured Application
Java Program to Implement Dijkstra’s Algorithm using Priority Queue
Spring Boot - Enabling Swagger2
Check if there is mail waiting
Java Deep Learning Essentials - Yusuke Sugomori
Extract network card address
How to Define a Spring Boot Filter?
Spring MVC + Thymeleaf 3.0: New Features
Phương thức tham chiếu trong Java 8 – Method References
Tạo ứng dụng Java RESTful Client với thư viện OkHttp
Partition a List in Java
Encode a String to UTF-8 in Java
Marker Interface trong Java
Hamcrest Collections Cookbook
Java Program to Implement Interval Tree
Spring 5 Functional Bean Registration
Java 8 Predicate Chain
What is Thread-Safety and How to Achieve it?
Guide to UUID in Java
Java Program to do a Depth First Search/Traversal on a graph non-recursively