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:
“Stream has already been operated upon or closed” Exception in Java
Java Program to Perform the Sorting Using Counting Sort
Instance Profile Credentials using Spring Cloud
Intro to Inversion of Control and Dependency Injection with Spring
LIKE Queries in Spring JPA Repositories
Hướng dẫn sử dụng luồng vào ra nhị phân trong Java
The Order of Tests in JUnit
Spring Boot - Thymeleaf
New in Spring Security OAuth2 – Verify Claims
Java Program to Construct an Expression Tree for an Postfix Expression
Lập trình đa luồng với CompletableFuture trong Java 8
Java Program to Check Cycle in a Graph using Topological Sort
Java Program to Implement Bresenham Line Algorithm
Error Handling for REST with Spring
Java Program to Implement Shunting Yard Algorithm
Send an email using the SMTP protocol
Java Program to Implement Rope
Java Switch Statement
Java Program to Implement Slicker Algorithm that avoids Triangulation to Find Area of a Polygon
Send an email with an attachment
Unsatisfied Dependency in Spring
Compare Two JSON Objects with Jackson
Java 8 and Infinite Streams
Spring Security OAuth Login with WebFlux
OAuth2 for a Spring REST API – Handle the Refresh Token in Angular
Một số tính năng mới về xử lý ngoại lệ trong Java 7
Java – Delete a File
Toán tử trong java
Mảng (Array) trong Java
The Java 8 Stream API Tutorial
Java Program to Implement ArrayList API
Tìm hiểu về xác thực và phân quyền trong ứng dụng