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:
Mảng (Array) trong Java
Using the Not Operator in If Conditions in Java
Spring Boot Integration Testing with Embedded MongoDB
Java Program to Implement Find all Cross Edges in a Graph
Lập trình đa luồng với Callable và Future trong Java
Prevent Cross-Site Scripting (XSS) in a Spring Application
Remove the First Element from a List
Hướng dẫn Java Design Pattern – Prototype
Java – Get Random Item/Element From a List
Spring Data Java 8 Support
Cài đặt và sử dụng Swagger UI
Spring Cloud AWS – Messaging Support
Custom Error Pages with Spring MVC
Hướng dẫn sử dụng Java Reflection
Java Program to Implement LinkedBlockingDeque API
Get and Post Lists of Objects with RestTemplate
Summing Numbers with Java Streams
Java Program to Implement Knight’s Tour Problem
OAuth 2.0 Resource Server With Spring Security 5
Java Program to Find the Median of two Sorted Arrays using Binary Search Approach
Spring Boot - Build Systems
Java Program to Implement ArrayDeque API
How to Replace Many if Statements in Java
Spring Boot - CORS Support
Mệnh đề if-else trong java
Using a List of Values in a JdbcTemplate IN Clause
Running Spring Boot Applications With Minikube
Spring Boot - Introduction
Removing all Nulls from a List in Java
Iterating over Enum Values in Java
Java Byte Array to InputStream
Java Program to Find Transpose of a Graph Matrix