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:

Lớp Properties trong java
Java Program to Implement Hash Tables with Quadratic Probing
Làm thế nào tạo instance của một class mà không gọi từ khóa new?
Java Program to Implement Sorted List
Jackson JSON Views
Java Program to Implement a Binary Search Algorithm for a Specific Search Sequence
Reversing a Linked List in Java
Java Program to Find the Minimum Element of a Rotated Sorted Array using Binary Search approach
Write/Read cookies using HTTP and Read a file from the internet
Hướng dẫn Java Design Pattern – Observer
Java Program to Implement Flood Fill Algorithm
Java Program to Compute the Area of a Triangle Using Determinants
Java Program to Implement Binary Heap
Removing all Nulls from a List in Java
Java Program to Implement Gift Wrapping Algorithm in Two Dimensions
Converting a List to String in Java
Hướng dẫn Java Design Pattern – Facade
Java Program to find the number of occurrences of a given number using Binary Search approach
Java Program to Implement Gauss Seidel Method
Tính đóng gói (Encapsulation) trong java
Java Program to Perform the Unique Factorization of a Given Number
Java Program to Generate a Sequence of N Characters for a Given Specific Case
Custom Error Pages with Spring MVC
Converting String to Stream of chars
Java Program to Perform Postorder Recursive Traversal of a Given Binary Tree
Spring Web Annotations
Disable DNS caching
Java Program to Generate Random Numbers Using Middle Square Method
What is a POJO Class?
Java – Convert File to InputStream
Lớp Collections trong Java (Collections Utility Class)
Java Program to Implement Circular Doubly Linked List