Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

architecture
binary
numbers
arithmetic

How to subtract two binary numbers

Educative Answers Team

In computer architecture, two binary numbers are subtracted using addition which avoids the need for a separate hardware to perform subtraction. For example, if 535-3 is to be calculated, then the operand on the right side (33) will need to be converted into its negative counterpart (3-3) and added to the other operand (55).

The two’s complement representation is the standard format for negative numbers.

Example

The steps below are to be followed when subtracting 1717 from 99:

  1. Convert both the numbers into binary format (the binary conversion table can be used).

9=(1001)9 = (1001)2

17=(10001)17 = (10001)2

  1. Make sure that both of the binary numbers have the same number of bits. If they do not, the number with the lesser bits will be extended. This is done by adding extra 00s to the left​ of the shorter number until the sizes become equal. In our example, 99 has four bits while 1717 has five bits, so we add one more 00 to the left making 9=(01001)9 = (01001)2.

  2. Convert 1717 into the two’s complement representation of 17-17 by inverting all of the bits and adding 11.

svg viewer
  1. Add 99 and 17-17 while ignoring the extra overflow bit from the answer.

When subtracting a larger number from a smaller one, we have to invert the ​bits of the answer and add 11 to find the magnitude. T​he sign is already known to be negative.

svg viewer

Magnitude: (01000)(01000)2 =8= 8.
Answer: -8

RELATED TAGS

architecture
binary
numbers
arithmetic
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring