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:
Giới thiệu Google Guice – Aspect Oriented Programming (AOP)
New Features in Java 8
Java Program to Find the Longest Path in a DAG
Using JWT with Spring Security OAuth
Java Byte Array to InputStream
Java Timer
Spring Boot - Introduction
Spring Security Form Login
Java Program to Implement Control Table
The SpringJUnitConfig and SpringJUnitWebConfig Annotations in Spring 5
Java Program to Give an Implementation of the Traditional Chinese Postman Problem
The Thread.join() Method in Java
Static Content in Spring WebFlux
CharSequence vs. String in Java
Java Program to Solve Set Cover Problem assuming at max 2 Elements in a Subset
Binary Numbers in Java
Dockerizing a Spring Boot Application
How to Return 404 with Spring WebFlux
Java Program to Implement Floyd Cycle Algorithm
Java Program to Describe the Representation of Graph using Adjacency Matrix
Java 8 Stream API Analogies in Kotlin
Introduction to Spring Cloud Netflix – Eureka
Spring MVC + Thymeleaf 3.0: New Features
Java Program to Perform Complex Number Multiplication
Java Program to Generate All Pairs of Subsets Whose Union Make the Set
Java Program to Emulate N Dice Roller
Java Program to Implement Sieve Of Sundaram
Java Program to Find Transpose of a Graph Matrix
Collection trong java
Spring @Primary Annotation
Map Interface trong java
Serverless Functions with Spring Cloud Function