Java Program to Perform Addition Operation Using Bitwise Operators

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:

Java Collections Interview Questions
Java Program to Use Boruvka’s Algorithm to Find the Minimum Spanning Tree
Spring Data Reactive Repositories with MongoDB
Hướng dẫn Java Design Pattern – Mediator
Tính trừu tượng (Abstraction) trong Java
Java Program to Show the Duality Transformation of Line and Point
Debugging Reactive Streams in Java
Spring Security Basic Authentication
Spring MVC Async vs Spring WebFlux
Java Program to subtract two large numbers using Linked Lists
Using JWT with Spring Security OAuth (legacy stack)
Sử dụng CyclicBarrier trong Java
Integer Constant Pool trong Java
Java Program to Check whether Graph is Biconnected
Easy Ways to Write a Java InputStream to an OutputStream
Java Program to Implement HashSet API
Creating a Custom Starter with Spring Boot
Java Program to Describe the Representation of Graph using Adjacency List
New Features in Java 8
A Guide to Apache Commons Collections CollectionUtils
Truyền giá trị và tham chiếu trong java
Java Program to Implement vector
Servlet 3 Async Support with Spring MVC and Spring Security
Spring Security 5 – OAuth2 Login
Spring Security OAuth2 – Simple Token Revocation
Java Program to Implement AA Tree
Java Program to Find SSSP (Single Source Shortest Path) in DAG (Directed Acyclic Graphs)
Overview of Spring Boot Dev Tools
Lập trình đa luồng với CompletableFuture trong Java 8
Java Program to Implement RoleList API
Java Program to Solve the 0-1 Knapsack Problem
Java Program to Check Cycle in a Graph using Topological Sort