Add two numbers without using arithmetic operators in java

Let’s learn to add two numbers without using arithmetic operators in java.

Add two numbers without using arithmetic operators in java

To add two numbers without using arithmetic operators first create function addNumber() which return sum of two numbers.

Add two numbers without using arithmetic operators in java

In addNumber() function we will not be using any arithmetic operators. Instead we are using bitwise operator XOR(^) of two bits.

Now let’s see program to add two numbers without using arithmetic operator in java.

public class AddWithoutArithmeticOperator
{
   static int addNumber(int a, int b)
   {
      while(b != 0)
      {
         int carry = a & b;
         a = a ^ b;
         b = carry << 1;
      }
      return a;
   }
   public static void main(String[] args)
   {
      System.out.println(addNumber(25, 25));
   }
}


Output:

50