Thursday, September 14, 2023

How to Subtract two Binary Numbers in Java? Example

Binary subtraction is very similar to binary addition which we have learned in the last article. In this tutorial, you will learn how to subtract two binary numbers. Similar to the last article, we'll see two ways, first by converting binary String to a binary number in Java and then doing subtraction. You can use the Java API Integer.toString(number, radix) for that. This is similar to convert String to Integer which we have seen in the past.  On the second solution, you will learn to develop the logic to perform binary subtraction in a Java program by using rules you might have learned in your computer classes. 

Here is a little summary of how binary subtraction works.  Subtraction works in much the same way as addition:

0 − 0 → 0
0 − 1 → 1, borrow 1
1 − 0 → 1
1 − 1 → 0

Subtracting a "1" digit from a "0" digit produces the digit "1", while 1 will have to be subtracted from the next column. 

This is known as borrowing. The principle is the same as for carrying. When the result of a subtraction is less than 0, the least possible value of a digit, the procedure is to "borrow" the deficit divided by the radix (that is, 10/10) from the left, subtracting it from the next positional value.

There are also a couple of other ways to perform binary subtraction like binary subtraction using 1's complement and binary subtraction using 2's complement. Let's see how those work by first subtracting two binary numbers using 1's complement:




How to subtract two binary numbers using 1's complement

In one's complement, you negate the binary number where 1 turned to zero and zero turned to 1. Here are the exact steps to subtract two binary numbers using 1's complement:

1. Calculate 1’s complement of the subtrahend.
2. Add 1's complement with the minuend.
3. If the result of addition has a carryover then it is dropped and a 1 is added in the last bit.
4. If there is no carryover, then 1’s complement of the result of the addition is obtained to get the final result and it is negative.

Example: What is the difference between two binary numbers 110101 – 100101?
Solution: 1’s complement of 10011 is 011010.
Hence

Minuted - 110101
1’s complement of subtrahend - 011010
Carryover - 1 001111
1
010000
The required difference is 10000

Here is another example of subtracting 101011 – 111001. First, let's calculate 1’s complement of 111001, which is 000110.
Hence

Minued - 101011
1’s complement - 000110
difference - 110001

Hence the difference between two binary numbers 101011 and 111001 is 1110. If you are not familiar with binary numbers and number systems, you can further check these Computer Science Fundamental courses to start with. 

How to Subtract two Binary Numbers in Java? Example


Java Program to subtract two binary numbers

import java.util.Scanner;

/*
 * Java Program to add subtract two binary numbers.
 * You can either write your own method or you 
 * can use Java API for doing binary subtraction.
 * 
 * input: 110101 - 100101
 * output = 1111
 */

public class Main {

  public static void main(String[] args) {

    System.out.println("Welcome to Java program to add two binary numbers");
    Scanner scnr = new Scanner(System.in);

    System.out.println("Please enter first binary number");
    String first = scnr.nextLine();

    System.out.println("Please enter second binary number");
    String second = scnr.nextLine();

    String difference = subtract(first, second);
    System.out.println("difference between two binary number is : "
        + difference);

    scnr.close();

  }

  /**
   * Java method to calculate the difference between 
   * two binary numbers this method
   * calculate sum by first converting binary String 
   * to binary numbers and then
   * subtracting them using binary arithmetic.
   * 
   * @param first
   * @param second
   * @return sum of two given binary numbers
   */
  public static String subtract(String first, String second) {
    int b1 = Integer.parseInt(first, 2);
    int b2 = Integer.parseInt(second, 2);
    int sum = b1 - b2;
    return Integer.toBinaryString(sum);
  }

}

Output
Welcome to Java program to add two binary numbers
Please enter the first binary number
110101
Please enter the second binary number
100101
difference between two binary number is: 10000


That's all about how to subtract two binary numbers in Java. This is a simple coding exercise for people learning programming and Java. Solving these kinds of programming exercises will help you learn programming fundamentals like variables, data types, operators, loops, conditional statements, functions, classes, etc. They will also help you to develop coding sense and problem solving skill required to solve a problem and write code for it. 

Lastly, what is your favorite Java programming exercise? Palindrom, Prime Number, Fibonacci series, Binary Search, or this one?  Do let me know in comments. 

1 comment:

  1. Using the same way given above, how to solve 10010 - 01100 ?

    ReplyDelete

Feel free to comment, ask questions if you have any doubt.