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.

Also read – continue statement in java

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


subtract two numbers without using arithmetic operators

Similarly we can subtract two numbers without using arithmetic operators.

To do that we have written static int function subtractNumber() to subtract two numbers which return subtraction of two numbers.

Also read – binary search in java

To subtract two numbers without using arithmetic operators we are using half subtractor logic.

So truth table for half subtractor is,

Input Output
A B Difference Borrow
0 0 0 0
0 1 1 1
1 0 1 0
1 1 0 0

Source – wiki

Here is the java program to subtract two numbers without using arithmetic operators.

public class SubtractDemo 
{
   static int subtractNumber(int i, int j) 
   { 
      while(j != 0) 
      { 
         int carry = (~i) & j; 
         i = i ^ j; 
         j = carry << 1; 
      }
      return i;
   }
   public static void main(String[] args) 
   {
      int a = 23, b = 10;
      System.out.println("a - b is " + subtractNumber(a, b));
   }
}


Output:

a – b is 13


multiply two numbers without using arithmetic operators

Also we can multiply two numbers without using arithmetic operators.

import java.util.Scanner;
public class MultiplyDemo
{
   static int multiplyNumber(int num1, int num2)
   {
      int output = 0;
      boolean boolNegative = (num1 < 0 && num2 >= 0) || (num2 < 0 && num1 >= 0);
      boolean boolPositive = !boolNegative;
      num1 = Math.abs(num1);
      for(int a = 0; a < num1; a++)
      {
         if(boolNegative && num2 > 0 || boolPositive && num2 < 0)
         {
            output -= num2;
         }
         else
         {
            output += num2;
         }
      }
      return output;
   }
   public static void main(String[] args)
   {
      Scanner sc = new Scanner(System.in);
      System.out.print("Please enter first number: ");
      int num1 = sc.nextInt();
      System.out.print("Please enter second number: ");
      int num2 = sc.nextInt();
      System.out.println("Multiplication of two numbers: " + multiplyNumber(num1, num2));
      sc.close();
   }
}


Output:

Please enter first number: 8
Please enter second number: 5
Multiplication of two numbers: 40