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 Program to Solve Tower of Hanoi Problem using Stacks
Java Program to Implement Fibonacci Heap
Java Program to Permute All Letters of an Input String
A Custom Data Binder in Spring MVC
Custom Thread Pools In Java 8 Parallel Streams
Hướng dẫn Java Design Pattern – State
Java Program to Implement Wagner and Fisher Algorithm for online String Matching
Java String Conversions
Java Program to Perform Postorder Recursive Traversal of a Given Binary Tree
Java Program to Find the Connected Components of an UnDirected Graph
Send email with JavaMail
Spring Boot - Google Cloud Platform
Spring Boot with Multiple SQL Import Files
Guide to Java 8 groupingBy Collector
Java Program to Check if an UnDirected Graph is a Tree or Not Using DFS
Guava CharMatcher
Java Program to Implement Network Flow Problem
Introduction to Spring Data MongoDB
Count Occurrences of a Char in a String
Arrays.asList vs new ArrayList(Arrays.asList())
Mix plain text and HTML content in a mail
Introduction to Java 8 Streams
Get and Post Lists of Objects with RestTemplate
Java Program to Perform Encoding of a Message Using Matrix Multiplication
Biến trong java
Java Program to Implement Hash Tables Chaining with List Heads
The Registration Process With Spring Security
Java Program to Repeatedly Search the Same Text (such as Bible by building a Data Structure)
Java Program to Compute Discrete Fourier Transform Using the Fast Fourier Transform Approach
List Interface trong Java
Receive email using POP3
Java Program to Check whether Directed Graph is Connected using BFS