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.

  1. //This is sample program to perform addition operation using bitwise operators.
  2. import java.util.Scanner;
  4. public class Bitwise_Addition 
  5. {
  6.     static int add(int x, int y)
  7.     {
  8.         int carry;
  9.         while(y!=0)
  10.         {
  11.             carry = x & y;
  12.             x = x ^ y;
  13.             y = carry << 1;
  14.         }
  15.         return x;
  16.     }
  17.     public static void main(String args[])
  18.     {
  19.         Scanner input = new Scanner(;
  20.         System.out.println("Enter the numbers to be added:");
  21.         int x = input.nextInt();
  22.         int y = input.nextInt();
  23.         System.out.println("The Summation is: "+add(x, y));
  24.         input.close();
  25.     }
  26. }


$ javac
$ java Bitwise_Addition
Enter the numbers to be added:
The Summation is: 31

Sanfoundry Global Education & Learning Series – 1000 Java Programs.


Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.

If you find any mistake above, kindly email to [email protected]

Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.