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:
Retrieve User Information in Spring Security
Hướng dẫn Java Design Pattern – Transfer Object
Spring REST API + OAuth2 + Angular
Java – Reader to String
Java Program to Implement Lloyd’s Algorithm
Using JWT with Spring Security OAuth (legacy stack)
Spring – Injecting Collections
Hướng dẫn sử dụng Java String, StringBuffer và StringBuilder
Java Program to Find the Mode in a Data Set
Java Program to Implement a Binary Search Algorithm for a Specific Search Sequence
Java 8 Predicate Chain
Java Program to Implement Euclid GCD Algorithm
Java Program to Perform Postorder Recursive Traversal of a Given Binary Tree
Limiting Query Results with JPA and Spring Data JPA
Java 8 StringJoiner
Exploring the New Spring Cloud Gateway
Flattening Nested Collections in Java
Java Program to Implement Hopcroft Algorithm
Tìm hiểu về Web Service
Java Program to Perform Right Rotation on a Binary Search Tree
Java Program to Implement Find all Forward Edges in a Graph
Guava CharMatcher
Configure a Spring Boot Web Application
Check If Two Lists are Equal in Java
Hướng dẫn Java Design Pattern – MVC
LinkedList trong java
Checking for Empty or Blank Strings in Java
Custom JUnit 4 Test Runners
Quick Guide to Spring Controllers
Validations for Enum Types
Handle EML file with JavaMail
Java Program to Find Location of a Point Placed in Three Dimensions Using K-D Trees