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

Add two numbers without using arithmetic operators in java

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

Also read – continue statement in java

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

Now let’s see java program to add two number without using arithmetic operator,

public class FB
{ 
   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


Similarly we can subtract two numbers without using arithmetic operators. To do that we have written static int method/function to subtract two numbers which return subtract of two numbers.

To subtract two numbers without using arithmetic operators we are using 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

Here is the java program to subtract two numbers without using arithmetic operator,

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


Also we can multiply two numbers without using arithmetic operators. Unlike above java program we make use of recursive function like this,

public class MultiplyDemo 
{
   static int multiplyNumber(int a, int b) 
   {    
      // 0 multiplied with 0 is 0
      if(b == 0) 
         return 0;

      // adding a one by one
      if(b > 0) 
         return (a + multiplyNumber(a, b - 1));

      // here's the case where b is negative
      if(b < 0) 
         return -multiplyNumber(a, -b); 

      return -1; 
   } 
   public static void main(String[] args) 
   {
      System.out.print(multiplyNumber(6, -10));
   }
}


Output:

-60


product of two numbers without using operator in java

Now let's write java program to multiply two integers without using multiply operator(*).

public class ProductWithoutOperator
{
   public static void main(String[] args) 
   {
      System.out.println("product of two numbers without using operator in java: ");
      System.out.println(product(5, 5));
   }

   static int product(int num1, int num2) 
   {
      if(num2 == 0) 
         return 0;
      if(num2 > 0)
         return (num1 + product(num1, num2 - 1));
      if(num2 < 0)
         return -product(num1, -num2);  
      return -1; 
   } 
}


Output:

product of two numbers without using operator in java: 25


multiply two numbers without using arithmetic operators

Let's learn to multiply two numbers without using arithmetic operators,

import java.util.Scanner;

public class MultiplyWithoutArithmeticOperators 
{
   public static void main(String[] args) 
   {
      Scanner sc = new Scanner(System.in);
      System.out.print("Please enter first number: ");
      int number1 = sc.nextInt();
      System.out.print("Please enter second number: ");
      int n2 = sc.nextInt();
      System.out.println("Multiply two numbers without using arithmetic operator: " + multiplyTwoNumbers(number1, n2));
      sc.close();
   }

   public static int multiplyTwoNumbers(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 i = 0; i < num1; i++)
      {
         if(boolNegative && num2 > 0 || boolPositive && num2 < 0)
         {
            output -= num2;
         }
         else
         {
            output += num2;
         }
      }
      return output;
   }
}


Output:

Please enter first number: 10
Please enter second number: 5
Multiply two numbers without using arithmetic operator: 50