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:
Adding a Newline Character to a String in Java
Hướng dẫn Java Design Pattern – Mediator
Java Program to Implement Hash Tables with Quadratic Probing
New in Spring Security OAuth2 – Verify Claims
Removing Elements from Java Collections
Unsatisfied Dependency in Spring
LIKE Queries in Spring JPA Repositories
Setting a Request Timeout for a Spring REST API
Java Program to Implement String Matching Using Vectors
A Guide to TreeMap in Java
SOAP Web service: Upload và Download file sử dụng MTOM trong JAX-WS
Database Migrations with Flyway
Spring Boot - Build Systems
Adding Shutdown Hooks for JVM Applications
More Jackson Annotations
Dynamic Proxies in Java
Receive email by java client
Creating a Web Application with Spring 5
Java Program to Represent Graph Using Linked List
Java Program to Find the GCD and LCM of two Numbers
New Features in Java 13
Hướng dẫn Java Design Pattern – State
Spring Boot - Enabling HTTPS
Spring Boot - Exception Handling
Converting Between a List and a Set in Java
Chuyển đổi giữa các kiểu dữ liệu trong Java
Java Program to find the peak element of an array using Binary Search approach
Java Program to implement Associate Array
Tính đóng gói (Encapsulation) trong java
Write/Read cookies using HTTP and Read a file from the internet
Java Program to Implement Gaussian Elimination Algorithm
Java Program to Find MST (Minimum Spanning Tree) using Kruskal’s Algorithm