Contents

Computer Arithmetic


Learning objective: To understand binary arithmetic operations and how to represent signed binary numbers.

Binary arithmetic

Like decimal numbers, we can also perform arithmetic operations on binary numbers. In this segment, we will talk about binary addition and subtraction.

Binary addition

There are 4 rules to take note of when performing binary addition:

A + B Sum Carry
0 + 0 0 0
0 + 1 1 0
1 + 0 1 0
1 + 1 0 1
  • In the fourth case, the sum which in this case is 0, is written in the given column and a carry of 1 is written over to the next column.
  • If the addition results in an extra bit, discard the extra bit.

Binary subtraction

Similarly, there are also 4 rules for binary subtraction:

A - B Result Borrow
0 - 0 0 0
1 - 0 1 0
1 - 1 0 0
0 - 1 0 1
  • Borrow in this case refers to subtracting 1 from the next column(if there is any) and “borrowing” 1 to the current column to perform the subtraction.

Examples

Binary addition:

/images/post/CSC1201/bin-add.png

Binary subtraction:

/images/post/CSC1201/bin-sub.png

Limitations

  • At their lowest level, computers cannot actually subtract, multiply or divide. It only performs addition at the bit level.

  • However, they can negate numbers using the XOR gate. Subtraction is the same as addition by the negative, i.e. A-B = A+(-B)

  • In order to do this, computers need to have a consistent medthod of representing signed binary numbers. This is when 1’s complement and 2’s complement method of representation come into play.

1’s Complement

  • The negative number of the same magnitude as any given positive number is its one’s complement. To do this, simply flip all the bits.
  • The range of numbers that can be represented with 1’s complement is -(2n-1)-1 to (2n-1)-1, where n is the number of bits.

Example:

The binary representation of 5 is 0101.

The 1’s complement representation of -5 is the 1010.

Note that for one’s complement and two’s complement, the MSB(most significant bit) indicates the sign. 0 means positive and 1 means negative.

Limitation:

  • One problem withh 1’s complement is that 0 can be either +0(0000) or -0(1111).
  • So when adding numbers using 1’s complement, you first have to perform binary addition, then add in an end-around carry value in order to get the correct answer:

/images/post/CSC1201/ones-add.jpg

2’s complement

  • Most modern computers will use 2’s complement to represent signed numbers instead of 1’s complement.

  • This is because in 2’s complement, there is only one representation of 0 so the result doesn’t require any modification. This avoids ambuguity and makes performing arithmetic simpler.

  • The range of numbers that can be represented with 1’s complement is -(2n-1) to (2n-1)-1, where n is the number of bits.

  • The 2’s complement of a negative value is its one’s complement plus one.

Examples:

  1. To convert -5 to 2’s complement, we first flip all the bits of its binary value then add one to it:
  • 0101 → 1010 → 1011.
  1. To convert 1011 in 2’s complement to decimal, we simply add the positional values together. Note that if the MSB is 1, then it’s positional value will be negative.
  • 1011 = -2$^3$ + 2$^1$ + 2$^0$ = -8 + 2 + 1 = -5

Binary arithmetic in 2’s complement

  • Adding the number is the same as regular binary addition.
  • To subtract b from a, take the 2’s complement of b and add it to a.

/images/post/CSC1201/twos-add.jpg