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 Check whether Graph is a Bipartite using DFS
Java – InputStream to Reader
Java Program to Print only Odd Numbered Levels of a Tree
Java Program to find the maximum subarray sum O(n^2) time(naive method)
Java Program to Perform Preorder Recursive Traversal of a Given Binary Tree
A Guide to @RepeatedTest in Junit 5
Java Program to Implement Hopcroft Algorithm
Setting Up Swagger 2 with a Spring REST API
Guide to Spring @Autowired
Java Program to Use the Bellman-Ford Algorithm to Find the Shortest Path
Giới thiệu Google Guice – Aspect Oriented Programming (AOP)
Java Program to Implement Borwein Algorithm
SOAP Web service: Upload và Download file sử dụng MTOM trong JAX-WS
Debugging Reactive Streams in Java
Getting Started with Stream Processing with Spring Cloud Data Flow
Hướng dẫn Java Design Pattern – Factory Method
Overview of the java.util.concurrent
A Guide to the ViewResolver in Spring MVC
Java Program to Implement Hash Tables Chaining with Doubly Linked Lists
Encode a String to UTF-8 in Java
Spring Security Basic Authentication
Overview of Spring Boot Dev Tools
Spring MVC Custom Validation
How to Break from Java Stream forEach
Java Program to Implement Binary Search Tree
Converting Iterator to List
Java Program to Implement RenderingHints API
Spring Boot - Batch Service
Concatenating Strings In Java
Spring Webflux with Kotlin
Multipart Upload with HttpClient 4
Creating a Custom Starter with Spring Boot