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

## Subtract two numbers without using arithmetic operators in java

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.

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 program on subtract two numbers without using arithmetic operators.

public class SubtractWithoutArithmeticOperators { 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 read – java overview