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:
Check if there is mail waiting
Loại bỏ các phần tử trùng trong một ArrayList như thế nào trong Java 8?
Hướng dẫn Java Design Pattern – Interpreter
A Custom Data Binder in Spring MVC
Java Program to implement Bi Directional Map
Spring Boot - Exception Handling
Java Program to Implement Counting Sort
Thao tác với tập tin và thư mục trong Java
Uploading MultipartFile with Spring RestTemplate
Java – Reader to Byte Array
Java Program to do a Depth First Search/Traversal on a graph non-recursively
Hướng dẫn sử dụng Java String, StringBuffer và StringBuilder
Java Program to Implement Gale Shapley Algorithm
Java Program to Implement ArrayBlockingQueue API
Spring Cloud – Bootstrapping
Java Program to Implement Pollard Rho Algorithm
Comparing Long Values in Java
Java Program to Check if a Given Binary Tree is an AVL Tree or Not
Auditing with JPA, Hibernate, and Spring Data JPA
Java Program to Solve the Fractional Knapsack Problem
Apache Commons Collections Bag
Disable DNS caching
Java Program to Implement Variable length array
Spring Boot - Google OAuth2 Sign-In
Java Program to Implement Aho-Corasick Algorithm for String Matching
Intro to the Jackson ObjectMapper
The Spring @Controller and @RestController Annotations
Collection trong java
Tránh lỗi ConcurrentModificationException trong Java như thế nào?
Guide to Java OutputStream
Java Program to Implement WeakHashMap API
Checked and Unchecked Exceptions in Java