Adding two decimal numbers involves a simple algorithm
Algorithm
Place the numbers on top of each other Right align the least significant digits Add the digits together, (as well as the carry)
Substraction is done in a similar manner, except that we don’t have carries, but borrows
Unsigned Binary Addition
Very similar :
Example
For us to represent the sum of two unsigned binary numbers of length , we need bits
We don't always have enough bits in hardware, we sometimes have a wrong result, and we say that an overflow has occurred
Unsigned Binary Subtraction
Same idea as for decimal numbers
Example
If we subtract a bigger number from a smaller number, but the result will be positive.
An underflow can happen when
Two’s Complement Addition and Subtraction
Addition is the same as unsigned addition
Overflows can still happen, but we can detect them
If the two operands have the same sign, but the result has a different sign, then overflow has happened
Two operands of different signs will never produce an overflow
Subtraction is addition of two signed numbers
Multiplication
Multiplication for decimal numbers is just adding a bunch of times the value to itself
Multiplication for Unsigned Binary Numbers
Algorithm
We add the number that is multiplied if the multiplicand is one We shift the multiplier And then we perform addition We repeat the process until we finish multiplying
Example
Multiplication for Signed Binary Numbers
We subtract a value which is the sign bit. (Add it via complement of sign + 1)
We redo the multiplication, by extending with the sign