-
CMPT 295
-
-
Unit - Data Representation
-
-
Lecture 4 – Representing integral numbers in memory – Arithmetic operations
-
-
Warm up question
-
- - What is the value of …
-
- - TMin (in hex) for signed char in C: _________________
- - TMax (in hex) for signed int in C: _________________
- - TMin (in hex) for signed short in C: ________________
-
-
-
-
-
Last Lecture
-
- - Interpretation of bit pattern B into either unsigned value U or signed value T
-
- - B2U(X) and U2B(X) encoding schemes (conversion)
- - B2T(X) and T2B(X) encoding schemes (conversion)
-
- - Signed value expressed as two’s complement => T
-
-
-
-
- - Conversions from unsigned <-> signed values
-
- - U2T(X) and T2U(X) => adding or subtracting 2w
-
-
- - Implication in C: when converting (implicitly via promotion and explicitly via casting):
-
- - Sign:
-
- - Unsigned <-> signed (of same size) -> Both have same bit pattern, however, this bit pattern may be interpreted differently
-
- - Can have unexpected effects -> producing a different value
-
-
-
-
- - Size:
-
- - Small -> large (for signed, e.g., short to int and for unsigned, e.g., unsigned short to unsigned int)
-
- - sign extension: For unsigned -> zeros extension, for signed -> sign bit extension
- - Both yield expected result –> resulting value unchanged
-
-
- - Large -> small (for signed, e.g., int to short and for unsigned, e.g., unsigned int to unsigned short)
-
- - truncation: Unsigned/signed -> most significant bits are truncated (discarded)
- - May not yield expected results -> original value may be altered
-
-
-
-
- - Both (sign and size): 1) size conversion is first done then 2) sign conversion is done
-
-
-
-
-
-
- - Representing data in memory – Most of this is review
-
- - “Under the Hood” - Von Neumann architecture
- - Bits and bytes in memory
-
- - How to diagram memory -> Used in this course and other references
- - How to represent series of bits -> In binary, in hexadecimal (conversion)
- - What kind of information (data) do series of bits represent -> Encoding scheme
- - Order of bytes in memory -> Endian
-
-
- - Bit manipulation – bitwise operations
-
- - Boolean algebra + Shifting
-
-
-
-
- - Representing integral numbers in memory
-
- - Unsigned and signed
- - Converting, expanding and truncating
- - Arithmetic operations
-
-
- - Representing real numbers in memory
-
- - IEEE floating point representation
- - Floating point in C – casting, rounding, addition, …
-
-
-
-
-
Let’s first illustrate what we covered last lecture with a demo!
-
-
Demo – Looking at size and sign conversions in C
-
-
- - What does the demo illustrate?
-
- - Size conversion:
-
- - Converting to a larger (wider) data type -> Converting short to int
- - Converting to a smaller (narrower) data type -> Converting short to char
-
-
- - Sign conversion:
-
- - Converting from signed to unsigned -> Converting short to unsigned short
- - Converting from unsigned to signed -> Converting unsigned short to short
-
-
- - Size and Sign conversion:
-
- - Converting from signed to unsigned larger (wider) data type -> Converting short to unsigned int
- - Converting from signed to unsigned smaller (narrower) data type ->
-Converting short to unsigned char
-
-
-
-
- - This demo (code and results) posted on our course web site
-
-
-
Integer addition (unlimited space)
-
-
- -
-
What happens when we add two decimal numbers? (Highlights show carring the one to complete the equation)
-10710+93810=104510
-
- -
-
Same thing happens when we add two binary numbers:
-
-
-
-
1011002+1011102=10110102
-
-
Unsigned addition (limited space, i.e., fixed size in memory)
-
-
What happens when we add two unsigned values?
-
-
w=8
-
-
a)
-
-
Binary:
-
-
001110112+010110102=?
-
-
Decimal:
-
-
5910+9010=14910
-
-
b)
-
-
Binary:
-
-
101011102+110010112=?
-
-
Decimal:
-
-
17410+20310=37710
-
-
Unsigned addition (+wu) and overflow
-
-
- - True/expected sum is the result of integer addition with unlimited space.
- - Actual sum is the result of unsigned addition with limited space. Discarding the carry out bit.
- - Discarding carry out bit has same effect as applying modular arithmetic
-
-
-
s=U+wuv=(u+v)mod2w
-
-
- - Operands: w bits
- - True Sum: w+1 bits
-
-
-
Closer look at unsigned addition overflow
-
-
- - w = 8 -> [0..255]
- -
- 25510=111111112
-
- -
- 9010=010110102
-
- -
- 4510=001011012
-
-
-
-
Example 1:
-
-
Decimal (carry out the 1 in 135):
-
-
9010+4510=13510
-
-
Binary (carry out the 1 in 100001112):
-
-
010110102+001011012=100001112
-
-
- - True sum, w=8: 100001112
- - Actual (overflowed) sum, w=8: 100001112=13510
-
-
-
Example 2:
-
-
Decimal (carry 1 to the 3 in 300):
-
-
25510+4510=30010
-
-
Binary (carry the 1 at the beginning of the result):
-
-
111111112+001011012=1001011002
-
-
- - True sum, w=9: 1001011002=30010
- - Actual (overflowed) sum, w=8: 001011002=4410
-
-
-
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) + 15 (11112) = 30 (111102) as a true sum, and 14 (111102) 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=?
-
-
Decimal:
-5910+9010=14910
-
-
b)
-
-
Binary: 101011102+110010112=?
-
-
Decimal: −8210+−5310=−13510
-
-
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) 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)
-
-
- - Discarding carry out bit has same effect as applying modular arithmetic
-
-
-
s=u+wtv=U2Tw[(u+v)mod2w]
-
-
Negative overflow and positive overflows are possible.
-Diagram showing negative overflows becoming positive, and positive overflows becoming negative.
-
-
Closer look at signed addition overflow
-
- - w = 8 -> [-128..127]
- -
- 9010=010110102
-
- -
- 4510=001011012
-
- -
- −4510=110100112
-
- -
- −9010=101001102
-
-
-
-
Example 1:
-
-
Decimal: 9010+4510=13510
-
-
Binary: 010110102+001011012=0100001112
-
-
The binary result is -121
-
-
Example 2:
-
-
Decimal: −9010+−4510=−13510
-
-
Binary: 101001102+110100112=1011110012
-
-
Binary result is 121
-
-
Example 3:
-
-
Decimal: −9010+4510=−4510
-
-
Binary: 101001102+001011012=0110100112
-
-
Example 4:
-
-
Decimal: 9010+−4510=4510
-
-
Binary: 010110102+110100112=1001011012
-
-
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) + 1 (00012) = 8 (10002) is the true sum and = -8 (10002) is the actual sum
-
-
What about subtraction? -> Addition
-
-
x + (-x) = 0
-
-
- - Subtracting a number is equivalent to adding its additive inverse
-
- - Instead of subtracting a positive number, we could add its negative version:
-
-
-
-
-
107−118=−11
-
-
becomes:
-
-
107+(−118)=−18
-
-
-
-
Decimal:
-
-
10710−11810=−11
-
-
Binary:
-
-
011010112−011101102=
-
-
Binary subtraction by addition:
-
-
011010112+100010102=111101012
-
-
Binary subtraction by addition is equal to -11
-
-
Check: −128+64+32+16+4+1=−1110
-
-
T2B(X) conversion:
-
- -
- ( (U2B(∣X∣)))+1
-
- -
- ( (U2B(∣−118∣)))+1
-
- -
- ( (U2B(118)))+1
-
- -
- (∼(011101102))+1
-
- -
- (100010012)+1
-
- -
- 100010102
-
-
-
-
Multiplication (∗wu,∗wt) 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.
-
-
- - Operands: w bits
- - True Product: 2w bits u×v
- -
-
Discard: w bits
-
- - Discarding high order w bits has same effect as applying modular arithmetic
-
-
-
p=u∗wuv=(u×v)mod2w
-
-
p=u∗wtv=U2Tw[(u×v)mod2w]
-
-
-
-
Decimal:
-
-
510×510=2510
-
-
Binary:
-
-
01012×01012=00110012
-
-
Multiplication with power-of-2 versus shifting
-
-
- - If x×y where y=2k then
x << k
-
- - For both signed and unsigned
-
-
- - Example:
-
- - x×8=x×23 =
x << 3
- - x×24=(x×25)–(x×23)=(x×32)–(x×8) = (x « 5) – (x « 3) (decompose 24 in powers of 2 => 32 – 8)
-
-
- - Most machines shift and add faster than multiply
-
- - We’ll soon see that compiler generates this code automatically
-17
-
-
-
-
-
Summary
-
-
- - Demo of size and sign conversion in C: code and results posted!
- - Addition:
-
- - Unsigned/signed:
-
- - Behave the same way at the bit level
- - Interpretation of resulting bit vector (sum) may differ
-
-
- - Unsigned addition -> may overflow, i.e., (w+1)th bit is set
-
- - If so, then actual sum obtained => (x+y)mod2w
-
-
- - Signed addition -> may overflow, i.e., (w+1)th bit is set
-
- - If so, then true sum may be too +ve -> positive overflow OR too -ve -> negative overflow
- - Then actual sum obtained => U2Tw[(x+y)mod2w]
-
-
-
-
- - Subtraction
-
- - Becomes an addition where negative operands are transformed into their additive inverse (in two’s complement)
-
-
- - Multiplication:
-
- - Unsigned: actual product obtained -> (x×y)mod2w
- - Signed: actual product obtained -> $$\text{U2T}_{w}[(x \times y) \mod 2^{w}]
- - Can be replaced by additions and shifts
-
-
-
-
-
Next lecture
-
- - Representing data in memory – Most of this is review
-
- - “Under the Hood” - Von Neumann architecture
- - Bits and bytes in memory
-
- - How to diagram memory -> Used in this course and other references
- - How to represent series of bits -> In binary, in hexadecimal (conversion)
- - What kind of information (data) do series of bits represent -> Encoding scheme
- - Order of bytes in memory -> Endian
-
-
- - Bit manipulation – bitwise operations
-
- - Boolean algebra + Shifting
-
-
-
-
- - Representing integral numbers in memory
-
- - Unsigned and signed
- - Converting, expanding and truncating
- - Arithmetic operations
-
-
- - Representing real numbers in memory
-
- - IEEE floating point representation
- - Floating point in C – casting, rounding, addition, …
-
-
-
-
-
We’ll illustrate what we covered today by having a demo!
-
-
-
-