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:
Spring Boot - Sending Email
Java Program to Find a Good Feedback Edge Set in a Graph
Guide to System.gc()
String Operations with Java Streams
Adding Parameters to HttpClient Requests
Java Program to Solve TSP Using Minimum Spanning Trees
Java Program to find the peak element of an array using Binary Search approach
Java Program to find the number of occurrences of a given number using Binary Search approach
Java Program to Perform Searching Using Self-Organizing Lists
Comparing getPath(), getAbsolutePath(), and getCanonicalPath() in Java
TreeSet và sử dụng Comparable, Comparator trong java
Java Program to Implement Floyd Cycle Algorithm
Java Program to Check Whether Graph is DAG
Tổng quan về ngôn ngữ lập trình java
A Guide to Queries in Spring Data MongoDB
Java – Convert File to InputStream
Logging in Spring Boot
Java Program to Implement Disjoint Sets
Java Collections Interview Questions
Java Program to Implement PriorityQueue API
Why String is Immutable in Java?
Spring MVC Custom Validation
ArrayList trong java
Java Program to Implement TreeSet API
Hướng dẫn sử dụng Lớp FilePermission trong java
Format ZonedDateTime to String
Java Program to Solve a Matching Problem for a Given Specific Case
Jackson – Change Name of Field
Java Program to Check whether Graph is a Bipartite using 2 Color Algorithm
Java Program to Implement the String Search Algorithm for Short Text Sizes
Spring Data Reactive Repositories with MongoDB
Cơ chế Upcasting và Downcasting trong java