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 \text{101100}_{2} + \text{101110}_{2} = \text{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=? \text{00111011}_{2} + \text{01011010}_{2} = ?

Decimal:

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

b)

Binary:

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

Decimal:

17410+20310=37710 \text{174}_{10} + \text{203}_{10} = \text{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 \text{90}_{10} + \text{45}_{10} = \text{135}_{10}

Binary (carry out the 1 in 100001112\text{10000111}_{2}):

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

Example 2:

Decimal (carry 1 to the 3 in 300):

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

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

111111112+001011012=1001011002 \text{11111111}_{2} + \text{00101101}_{2} = \text{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 (11112\text{1111}_{2}) + 15 (11112\text{1111}_{2}) = 30 (111102\text{11110}_{2}) as a true sum, and 14 (111102\text{11110}_{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=?\text{00111011}_{2} + \text{01011010}_{2} = ?

Decimal: 5910+9010=14910\text{59}_{10} + \text{90}_{10} = \text{149}_{10}

b)

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

Decimal: -8210+-5310=-13510\text{-82}_{10} + \text{-53}_{10} = \text{-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=13510\text{90}_{10} + \text{45}_{10} = \text{135}_{10}

Binary: 010110102+001011012=0100001112\text{01011010}_{2} + \text{00101101}_{2} = \text{010000111}_{2}

The binary result is -121

Example 2:

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

Binary: 101001102+110100112=1011110012\text{10100110}_{2} + \text{11010011}_{2} = \text{101111001}_{2}

Binary result is 121

Example 3:

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

Binary: 101001102+001011012=0110100112\text{10100110}_{2} + \text{00101101}_{2} = \text{011010011}_{2}

Example 4:

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

Binary: 010110102+110100112=1001011012\text{01011010}_{2} + \text{11010011}_{2} = \text{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 (01112\text{0111}_{2}) + 1 (00012\text{0001}_{2}) = 8 (10002\text{1000}_{2}) is the true sum and = -8 (10002\text{1000}_{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=11\text{107}_{10} - \text{118}_{10} = -11

Binary:

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

Binary subtraction by addition:

011010112+100010102=111101012\text{01101011}_{2} + \text{10001010}_{2} = \text{11110101}_{2}

Binary subtraction by addition is equal to -11

Check: 128+64+32+16+4+1=-1110-128 + 64 + 32 + 16 + 4 + 1 = \text{-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(\text{10001001}_{2})+1
  6. 100010102\text{10001010}_{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} = \text{25}_{10}

Binary:

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

Multiplication with power-of-2 versus shifting

Summary

Next lecture

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