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:

Inheritance with Jackson
Spring Boot - Hystrix
Model, ModelMap, and ModelAndView in Spring MVC
Java Program to Implement Disjoint Set Data Structure
Instance Profile Credentials using Spring Cloud
Get and Post Lists of Objects with RestTemplate
HashSet trong Java hoạt động như thế nào?
An Intro to Spring Cloud Contract
Java Program to Implement Weight Balanced Tree
Java Program to Implement the linear congruential generator for Pseudo Random Number Generation
Jackson Unmarshalling JSON with Unknown Properties
Java Program to Implement Regular Falsi Algorithm
Java Program to Implement Sorted Circular Doubly Linked List
Spring Boot - Code Structure
Java Program to Implement the Hill Cypher
Java Program to Implement Knight’s Tour Problem
Java Program to Implement Segment Tree
The Spring @Controller and @RestController Annotations
The Java 8 Stream API Tutorial
Java Program to Check Whether a Directed Graph Contains a Eulerian Path
Convert Time to Milliseconds in Java
Exploring the New Spring Cloud Gateway
Java Program to Implement ScapeGoat Tree
Java Program to Find k Numbers Closest to Median of S, Where S is a Set of n Numbers
Java Program to Perform Inorder Recursive Traversal of a Given Binary Tree
How to Return 404 with Spring WebFlux
Intro to the Jackson ObjectMapper
Java Program to Implement the One Time Pad Algorithm
Java Program to Delete a Particular Node in a Tree Without Using Recursion
Different Ways to Capture Java Heap Dumps
A Guide to Spring Boot Admin
Generating Random Numbers in a Range in Java