Integer Arithmetic

Unsigned Addition

Given Bits, Bytes and Integers Part 2 - 图1, the result of truncating the integer sum Bits, Bytes and Integers Part 2 - 图2 to be Bits, Bytes and Integers Part 2 - 图3 bits long and then viewing the result as an unsigned number.
image.png

Bits, Bytes and Integers Part 2 - 图5 modular arithmetic: compute the sum modulo Bits, Bytes and Integers Part 2 - 图6 by simply discarding any bits with weight greater than Bits, Bytes and Integers Part 2 - 图7 in the bit-level representation of Bits, Bytes and Integers Part 2 - 图8.

We can characterize operation Bits, Bytes and Integers Part 2 - 图9 as follows:
image.png

Note:

When Bits, Bytes and Integers Part 2 - 图11, the leading bit in the Bits, Bytes and Integers Part 2 - 图12#card=math&code=%28w%2B1%29&height=18&width=48) bits representation of the sum will equal Bits, Bytes and Integers Part 2 - 图13, and hence discarding it will not change the numeric value. On the other hand, if Bits, Bytes and Integers Part 2 - 图14, the leading bit in the Bits, Bytes and Integers Part 2 - 图15#card=math&code=%28w%2B1%29&height=18&width=48) bits representation of the sum will equal Bits, Bytes and Integers Part 2 - 图16, and hence discarding it is equivalent to subtracting Bits, Bytes and Integers Part 2 - 图17 from the sum.
image.png
image.png

Two’s Complement Addition

With two’s complement addition, we must decide what to do when the result is
either too large (positive) or too small (negative) to represent.

Given integer values Bits, Bytes and Integers Part 2 - 图20 and Bits, Bytes and Integers Part 2 - 图21 in the range Bits, Bytes and Integers Part 2 - 图22, their sum is in the range Bits, Bytes and Integers Part 2 - 图23, potentially requiring Bits, Bytes and Integers Part 2 - 图24 bits to represent exactly.
image.pngimage.png
image.png

Two’s Complement Negation

image.png

Note:

Observing that Bits, Bytes and Integers Part 2 - 图29. This would cause negative overflow, and Bits, Bytes and Integers Part 2 - 图30. For value of Bits, Bytes and Integers Part 2 - 图31 such that Bits, Bytes and Integers Part 2 - 图32, the value Bits, Bytes and Integers Part 2 - 图33 can therefore be represented by Bits, Bytes and Integers Part 2 - 图34 bits two’s complement number, and their sum will be Bits, Bytes and Integers Part 2 - 图35.

Unsigned Multiplication

Integers Bits, Bytes and Integers Part 2 - 图36 and Bits, Bytes and Integers Part 2 - 图37 in the range Bits, Bytes and Integers Part 2 - 图38 can be represented as Bits, Bytes and Integers Part 2 - 图39 bits unsigned numbers, but their product Bits, Bytes and Integers Part 2 - 图40 can range between Bits, Bytes and Integers Part 2 - 图41 and Bits, Bytes and Integers Part 2 - 图42%5E2%3D2%5E%7B2w%7D-2%5E%7Bw%2B1%7D%20%2B1#card=math&code=%282%5Ew%20-1%29%5E2%3D2%5E%7B2w%7D-2%5E%7Bw%2B1%7D%20%2B1&height=20&width=177). This could require as many as Bits, Bytes and Integers Part 2 - 图43 bits to represent. image.png

Two’s Complement Multiplication

Integers Bits, Bytes and Integers Part 2 - 图45 and Bits, Bytes and Integers Part 2 - 图46 in the range Bits, Bytes and Integers Part 2 - 图47 can be represented as Bits, Bytes and Integers Part 2 - 图48 bits two’s complement numbers, but their product Bits, Bytes and Integers Part 2 - 图49 can range between Bits, Bytes and Integers Part 2 - 图50%3D-2%5E%7B2w-2%7D%2B2%5E%7Bw-1%7D#card=math&code=-2%5E%7Bw-1%7D%5Ccdot%20%282%5E%7Bw-1%7D-1%29%3D-2%5E%7B2w-2%7D%2B2%5E%7Bw-1%7D&height=20&width=234). This could require as many as Bits, Bytes and Integers Part 2 - 图51 bits to represent in two’s complement form.
image.png

Note:

When multiplying by a power of 2, one important optimization used by compilers is to combine shift and addition operations.
image.png
This property can be derived using:

Bits, Bytes and Integers Part 2 - 图54%3D%5Csum%7Bi%3D0%7D%5E%7Bw-1%7Dx_i%202%5E%7Bi%2Bk%7D%3Dx%202%5Ek%0A#card=math&code=B2U%7Bw%2Bk%7D%28%5Bx%7Bw-1%7D%2Cx%7Bw-2%7D%2C…%2Cx0%2C0%2C…%2C0%5D%29%3D%5Csum%7Bi%3D0%7D%5E%7Bw-1%7Dx_i%202%5E%7Bi%2Bk%7D%3Dx%202%5Ek%0A&height=47&width=371)

Also, dividing by a power of 2 can also be performed using shift operations. This time we use a right shift rather than a left shift.
image.png

Why Should I Use Unsigned?

Don’t use without understanding implications

  • Easy to make mistakes

    1. unsigned i;
    2. for (i = cnt-2; i >= 0; i--)
    3. a[i] += a[i+1];
  • Can be very subtle

    #define DELTA sizeof(int)
    int i;
    for (i = CNT; i-DELTA >= 0; i-= DELTA)
      . . .