CMPT 295

Unit - Data Representation

Lecture 4 – Representing integral numbers in memory – Arithmetic operations

Warm up question

Last Lecture

Today’s Menu

Let’s first illustrate what we covered last lecture with a demo!

Demo – Looking at size and sign conversions in C

Integer addition (unlimited space)

1011002+1011102=10110102 101100_{2} + 101110_{2} = 1011010_{2}

Unsigned addition (limited space, i.e., fixed size in memory)

What happens when we add two unsigned values?

w=8w=8

a)

Binary:

001110112+010110102=? 00111011_{2} + 01011010_{2} = ?

Decimal:

5910+9010=14910 59_{10} + 90_{10} = 149_{10}

b)

Binary:

101011102+110010112=? 10101110_{2} + 11001011_{2} = ?

Decimal:

17410+20310=37710 174_{10} + 203_{10} = 377_{10}

Unsigned addition (+wu+^{u}_{w}) and overflow

s=U+wuv=(u+v)mod2w s = U+^{u}_{w}v = (u + v) \mod{2^{w}}

Closer look at unsigned addition overflow

Example 1:

Decimal (carry out the 1 in 135):

9010+4510=13510 90_{10} + 45_{10} = 135_{10}

Binary (carry out the 1 in 10000111210000111_{2}):

010110102+001011012=100001112 01011010_{2} + 00101101_{2} = 10000111_{2}

Example 2:

Decimal (carry 1 to the 3 in 300):

25510+4510=30010 255_{10} + 45_{10} = 300_{10}

Binary (carry the 1 at the beginning of the result):

111111112+001011012=1001011002 11111111_{2} + 00101101_{2} = 100101100_{2}

Comparing integer addition with Overflow: Effect of unsigned addition (w = 4)

Annotation: with unlimited space:

A 3d chart showing two numbers being added. a and b on the z and x axies, the sum on the y axis. y goes to a maximum height of 32 (a = 15) + (b = 15) = (y = 30) Annotation: With limited space (fixed-size memory):

A 3d chart showing two numbers being added. a and b on the z and x axies, the sum on the y axis. y goes to a maximum height of 15 (a = 15) + (b = 15) = (y = 14)

An overflow occurs when there is a carry out

For example: 15 (111121111_{2}) + 15 (111121111_{2}) = 30 (11110211110_{2}) as a true sum, and 14 (11110211110_{2}) as an actual sum.

Signed addition (limited space, i.e., fixed size in memory)

What happens when we add two signed values:

w=8

a)

Binary: 001110112+010110102=?00111011_{2} + 01011010_{2} = ?

Decimal: 5910+9010=1491059_{10} + 90_{10} = 149_{10}

b)

Binary: 101011102+110010112=?10101110_{2} + 11001011_{2} = ?

Decimal: 8210+5310=13510-82_{10} + -53_{10} = -135_{10}

Observation: Unsigned and signed additions have identical behavior @ the bit level, i.e., their sum have the same bit-level representation, but their interpretation differs

Signed addition (+wt+^{t}_{w}) and overflow

True sum would be the result of integer addition with unlimited space: Actual sum is the result of signed addition with limited space:

Operands: w bits True Sum: w+1 bits

After discarduing carry out bit: w bits (overflow)

s=u+wtv=U2Tw[(u+v)mod2w] s = u +^{t}_{w} v = \text{U2T}_{w}[(u + v) \mod 2^{w}]

Negative overflow and positive overflows are possible. Diagram showing negative overflows becoming positive, and positive overflows becoming negative.

Closer look at signed addition overflow

Example 1:

Decimal: 9010+4510=1351090_{10} + 45_{10} = 135_{10}

Binary: 010110102+001011012=010000111201011010_{2} + 00101101_{2} = 010000111_{2}

The binary result is -121

Example 2:

Decimal: 9010+4510=13510-90_{10} + -45_{10} = -135_{10}

Binary: 101001102+110100112=101111001210100110_{2} + 11010011_{2} = 101111001_{2}

Binary result is 121

Example 3:

Decimal: 9010+4510=4510-90_{10} + 45_{10} = -45_{10}

Binary: 101001102+001011012=011010011210100110_{2} + 00101101_{2} = 011010011_{2}

Example 4:

Decimal: 9010+4510=451090_{10} + -45_{10} = 45_{10}

Binary: 010110102+110100112=100101101201011010_{2} + 11010011_{2} = 100101101_{2}

A chart showing the relationship between true sum and actual (overflowed) sum. Actual sum has a possible value between 127 to -128. A true sum, however has a range between 255 and -256. As your true sum goes further down from -128, its actual sum becomes lower as well. Starting at -129 = 127. As you true sum goes futher up from 127, the actual sum also rises, satrting with 128 = -128.

Visualizing signed addition overflow (w = 4)

A 3D chart which has its x axis go from -8 to +7, its z axis go from -8 to +6, and a y axis which goes from :wq

Positive Overflow

For example: 7 (011120111_{2}) + 1 (000120001_{2}) = 8 (100021000_{2}) is the true sum and = -8 (100021000_{2}) is the actual sum

What about subtraction? -> Addition

x + (-x) = 0

107118=11 107 - 118 = -11

becomes:

107+(118)=18 107 + (-118) = -18

Decimal:

1071011810=11107_{10} - 118_{10} = -11

Binary:

011010112011101102=01101011_{2} - 01110110_{2} =

Binary subtraction by addition:

011010112+100010102=11110101201101011_{2} + 10001010_{2} = 11110101_{2}

Binary subtraction by addition is equal to -11

Check: 128+64+32+16+4+1=1110-128 + 64 + 32 + 16 + 4 + 1 = -11_{10}

T2B(X) conversion:

  1. ( (U2B(X)))+1(~(\text{U2B}(|X|)))+1
  2. ( (U2B(118)))+1(~(\text{U2B}(|-118|)))+1
  3. ( (U2B(118)))+1(~(\text{U2B}(118)))+1
  4. ((011101102))+1(\sim(01110110_{2}))+1
  5. (100010012)+1(10001001_{2})+1
  6. 10001010210001010_{2}

Multiplication (wu,wt*^{u}_{w}, *^{t}_{w}) and overflow

True product would be the result of integer multiplication with unlimited space: expected product Actual product is the result of multiplication with limited space.

p=uwuv=(u×v)mod2wp = u *^{u}_{w}v = (u \times v) \mod 2^{w} p=uwtv=U2Tw[(u×v)mod2w]p = u *^{t}_{w}v = \text{U2T}_{w}[(u \times v) \mod 2^{w}]

Decimal:

510×510=25105_{10} \times 5_{10} = 25_{10}

Binary:

01012×01012=001100120101_{2} \times 0101_{2} = \sout{001}1001_{2}

Multiplication with power-of-2 versus shifting

Summary

Next lecture

We’ll illustrate what we covered today by having a demo!