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

ReprRepr()~Repr()~Repr()+1
00000000111000
0011-1111110111
0102-2110101110
0113-3101100101
100-4-4100011100

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.

s01
001
110
+01
00001
10110
C01
000
101

-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