Integer Arithmetic
We want to perform the standard collection of integer arithmetic operations: addition , unary negation/additive inverse , subtraction , multiplication , division .
- Everything can be defined from addition and additive inverse
We can’t do mathematical addition with -bit representations
- We defined the operations ,
Unsigned Integer Addition:
Operationally:
- Starting at position on the unsigned -wheel, move positions clockwise, ending at the sum
Definition:
If we go past the red flag (largest representable value) in doing this, we have an unsigned carry.
Properties of Integer Addition
is commutative and associative
The element is the unique identity for
- i.e., .
Every element has a unique additive inverse
- i.e.,
Unsigned Integer Subtraction:
Operationally:
- Starting at position on the unsigned -wheel, move positions clockwise (equivalent to moving positions counter-clockwise), ending at the difference
Definition:
If we go past the red flag in doing this, we have an unsigned carry.
Same properties as integer addition.
Signed Integer Addition:
Operationally:
- Starting at position on the signed -wheel, move positions clockwise, ending at the sum.
- What if ?
Definition:
- Let , this is the mathematical sum
- Then
Signed Overflow:
If we go past the red flag in either direction while doing this, we have a signed overflow
Detection is value based
Properties of
is commutative and associative
The element is the unique identity for
- i.e.,
Every element has a unique additive inverse
- i.e.,
Example Additive Inverse
Repr | Repr() | ~Repr() | ~Repr()+1 | ||
---|---|---|---|---|---|
000 | 0 | 0 | 000 | 111 | 000 |
001 | 1 | -1 | 111 | 110 | 111 |
010 | 2 | -2 | 110 | 101 | 110 |
011 | 3 | -3 | 101 | 100 | 101 |
100 | -4 | -4 | 100 | 011 | 100 |
Multiplication: and
Operationally (not very useful):
- Iterated addition — literally definitionally what multiplication is
- Adjust appropriately for signed
Definition:
- Unsigned:
- Signed:
- First calculate the unsigned value, then convert to a signed value.
means Unsigned to Signed - We need this one because of the definition of the mod operator.
means Signed to Unsigned
Ex: with and
Ex: with and
Ex: with and
The number’s and are chosen because we have defined to not be negative. So we must go farther and then step back.
In this case, we go to and then move upwards back to .
Multiplication by Constants
For :
This gives us a way of simplifying multiplying by constants using shifts and adds (strength reduction).
Ex: .
Floor and Ceiling Functions
For any real number , we define:
- floor(), written , to be the greatest integer less than or equal to
- ceil(), written , to be the smallest integer greater than or equal to
If is an integer, then
Otherwise,
Integer Division by Constants
For :
- Unsigned: yields
- Signed: yields
- Signed: yields
- We first bias the value and then perform the shift
Implementing Addition
Very, very similar to a grade school addition algorithm.
s | 0 | 1 |
---|---|---|
0 | 0 | 1 |
1 | 1 | 0 |
+ | 0 | 1 |
---|---|---|
0 | 00 | 01 |
1 | 01 | 10 |
C | 0 | 1 |
---|---|---|
0 | 0 | 0 |
1 | 0 | 1 |
-bit Adder
We want
Literally think of regular addition but in base 2, so instead of reaching 10 and then carrying, we carry when we reach 2.
We use the ripple-carry adder scheme
-bit Subtractor
We want
We will implement this by using the definition of subtraction in terms of addition and additive inverse:
Literally think of regular addition but in base 2, so instead of reaching 10 and then carrying, we carry when we reach 2.
We still use the ripple-carry adder scheme