A computer performs every calculation, from a bill total to a game score, in binary. Understanding binary addition and subtraction shows why a processor needs only an adder and a method of forming complements.

Binary Addition

Binary addition follows four rules: 0 + 0 = 0, 0 + 1 = 1, 1 + 0 = 1 and 1 + 1 = 10, which is 0 with a carry of 1. For example 1011 + 0110 gives 10001, which is 11 + 6 = 17 in decimal.

Binary Subtraction by Complement

Subtraction is carried out by adding the 2's complement of the number to be subtracted. The 2's complement is formed by inverting every bit and adding 1. If a carry comes out of the leftmost position, it is discarded.

Definition: 2's complement. The value obtained by inverting every bit of a binary number and adding 1. Adding the 2's complement of a number is equivalent to subtracting that number.

Example 1.8: Subtraction using 2's complement

Problem: Subtract 0110 (6) from 1101 (13) using 4-bit 2's complement.

Solution:

  1. Invert 0110 to get 1001.
  2. Add 1 to get 1010, which is the 2's complement of 0110.
  3. Add 1101 + 1010 = 10111.
  4. Discard the carry out of the fourth bit, which leaves 0111.

Result: 13 - 6 = 0111, which is 7.

Octal and Hexadecimal Arithmetic

Octal and hexadecimal numbers are added digit by digit like decimal numbers. In octal a carry is generated when a column total reaches 8, and in hexadecimal when it reaches 16. For example 57 + 26 in octal gives 105, and 2F + 1B in hexadecimal gives 4A.

print(bin(0b1011 + 0b0110)[2:])
print(oct(0o57 + 0o26)[2:], hex(0x2F + 0x1B)[2:].upper())
print(bin((13 + (16 - 6)) % 16)[2:].zfill(4))

Output

10001
105 4A
0111

BCD Addition

In binary coded decimal (BCD) each decimal digit is written as four bits. When two BCD digits are added, a result greater than 1001 (9), or a carry out, is not a valid BCD digit. In that case 0110 (6) is added to correct the group.

Table 1.8: BCD addition rules

Step Action Reason
1 Add the two 4-bit groups in binary Plain binary addition
2 Check the sum Greater than 9 or a carry
3 If so, add 0110 Skips the six unused codes
4 Carry to the next digit Same as decimal carry

Example 1.9: Adding two BCD numbers

Problem: Add 47 and 38 in BCD.

Solution:

  1. 47 in BCD is 0100 0111 and 38 is 0011 1000.
  2. Units: 0111 + 1000 = 1111, which is greater than 9. Add 0110: 1111 + 0110 = 1 0101. The units digit is 0101 and the carry is 1.
  3. Tens: 0100 + 0011 + 1 = 1000.

Result: The sum is 1000 0101, which is 85.

BCD Subtraction

BCD subtraction is carried out with the 9's complement or the 10's complement of the subtrahend digits. The 9's complement of a digit d is 9 - d. This follows the same idea as binary complements, with the carry added back or discarded as the method requires.

Common mistakes

  • Forgetting to discard the final carry in 2's complement subtraction.
  • Adding 0110 to a BCD digit that is already valid.
  • Treating a hexadecimal letter as its position in the alphabet, not as a value from 10 to 15.

Key takeaways

  • Binary: 1 + 1 = 10 (0 with carry 1).
  • Subtraction is done by adding the 2's complement.
  • Octal carries at 8 and hexadecimal carries at 16.
  • BCD addition needs a correction of 0110 when a digit exceeds 9.

Practice questions

  1. Add 1010 and 0101 in binary.
  2. How is the 2's complement of a binary number formed?
  3. When is 0110 added in BCD addition?

Answers to the practice questions

  1. Invert every bit and add 1.
  2. When a digit sum is greater than 9 or produces a carry.

Sources

  • Official BCA syllabus Computer Architecture, course code 25BCA401DS02, Unit I