12 KiB
layout | math |
---|---|
simple | true |
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
- Unsigned <-> signed (of same size) -> Both have same bit pattern, however, this bit pattern may be interpreted differently
- 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
- Small -> large (for signed, e.g., short to int and for unsigned, e.g., unsigned short to unsigned int)
- Both (sign and size): 1) size conversion is first done then 2) sign conversion is done
- Sign:
Today’s Menu
- 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
- Size conversion:
-
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) {% katex display %} 107_{10} + 938_{10} = 1045_{10}{% endkatex %}
-
Same thing happens when we add two binary numbers:
{% katex display %} 101100_{2} + 101110_{2} = 1011010_{2} {% endkatex %}
Unsigned addition (limited space, i.e., fixed size in memory)
What happens when we add two unsigned values?
w=8
a)
Binary:
{% katex display %} 00111011_{2} + 01011010_{2} = ? {% endkatex %}
Decimal:
{% katex display %} 59_{10} + 90_{10} = 149_{10} {% endkatex %}
b)
Binary:
{% katex display %} 10101110_{2} + 11001011_{2} = ? {% endkatex %}
Decimal:
{% katex display %} 174_{10} + 203_{10} = 377_{10} {% endkatex %}
Unsigned addition (
+^{u}_{w}
) 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
{% katex display %} s = U+^{u}_{w}v = (u + v) \mod{2^{w}} {% endkatex %}
- Operands: w bits
- True Sum: w+1 bits
Closer look at unsigned addition overflow
- w = 8 -> [0..255]
-
255_{10} = 11111111_{2}
-
90_{10} = 01011010_{2}
-
45_{10} = 00101101_{2}
Example 1:
Decimal (carry out the 1 in 135):
{% katex display %} 90_{10} + 45_{10} = 135_{10} {% endkatex %}
Binary (carry out the 1 in $10000111_{2}
$):
{% katex display %} 01011010_{2} + 00101101_{2} = 10000111_{2} {% endkatex %}
- True sum, w=8:
$10000111_{2}
$ - Actual (overflowed) sum, w=8:
$10000111_{2} = 135_{10}
$
Example 2:
Decimal (carry 1 to the 3 in 300):
{% katex display %} 255_{10} + 45_{10} = 300_{10} {% endkatex %}
Binary (carry the 1 at the beginning of the result):
{% katex display %} 11111111_{2} + 00101101_{2} = 100101100_{2} {% endkatex %}
- True sum, w=9:
$100101100_{2} = 300_{10}
$ - Actual (overflowed) sum, w=8:
$00101100_{2} = 44_{10}
$
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 ($1111_{2}
) + 15 (
1111_{2}
) = 30 (
11110_{2}
) as a true sum, and 14 (
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:
00111011_{2} + 01011010_{2} = ?
Decimal:
59_{10} + 90_{10} = 149_{10}
b)
Binary: $10101110_{2} + 11001011_{2} = ?
$
Decimal: -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 (
+^{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)
- Discarding carry out bit has same effect as applying modular arithmetic
{% katex display %} s = u +^{t}{w} v = \text{U2T}{w}[(u + v) \mod 2^{w}] {% endkatex %}
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]
-
90_{10} = 01011010_{2}
-
45_{10} = 00101101_{2}
-
-45_{10} = 11010011_{2}
-
-90_{10} = 10100110_{2}
Example 1:
Decimal: $90_{10} + 45_{10} = 135_{10}
$
Binary: $01011010_{2} + 00101101_{2} = 010000111_{2}
$
The binary result is -121
Example 2:
Decimal: -90_{10} + -45_{10} = -135_{10}
Binary: $10100110_{2} + 11010011_{2} = 101111001_{2}
$
Binary result is 121
Example 3:
Decimal: -90_{10} + 45_{10} = -45_{10}
Binary: $10100110_{2} + 00101101_{2} = 011010011_{2}
$
Example 4:
Decimal: $90_{10} + -45_{10} = 45_{10}
$
Binary: $01011010_{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 ($0111_{2}
) + 1 (
0001_{2}
) = 8 (
1000_{2}
) is the true sum and = -8 (
1000_{2}
$) 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:
{% katex display %} 107 - 118 = -11 {% endkatex %}
becomes:
{% katex display %} 107 + (-118) = -18 {% endkatex %}
- Let‘s try:
Decimal:
107_{10} - 118_{10} = -11
Binary:
01101011_{2} - 01110110_{2} =
Binary subtraction by addition:
01101011_{2} + 10001010_{2} = 11110101_{2}
Binary subtraction by addition is equal to -11
Check: -128 + 64 + 32 + 16 + 4 + 1 = -11_{10}
T2B(X) conversion:
-
(~(\text{U2B}(|X|)))+1
-
(~(\text{U2B}(|-118|)))+1
-
(~(\text{U2B}(118)))+1
-
(\sim(01110110_{2}))+1
-
(10001001_{2})+1
-
10001010_{2}
Multiplication (
*^{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.
-
Operands: w bits
-
True Product: 2w bits
$u \times v
$ -
Discard: w bits
-
Discarding high order w bits has same effect as applying modular arithmetic
p = u *^{u}_{w}v = (u \times v) \mod 2^{w}
p = u *^{t}_{w}v = \text{U2T}_{w}[(u \times v) \mod 2^{w}]
- Example: w = 4
Decimal:
5_{10} \times 5_{10} = 25_{10}
Binary:
0101_{2} \times 0101_{2} = \sout{001}1001_{2}
Multiplication with power-of-2 versus shifting
- If
$x \times y
where
y = 2^{k}
$ thenx << k
- For both signed and unsigned
- Example:
-
x \times 8 = x \times 2^{3}
-
x \times 24 = (x \times 25) – (x \times 23) = (x \times 32) – (x \times 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) \mod 2^{w}
- If so, then actual sum obtained =>
- 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 =>
\text{U2T}_{w}[(x + y) \mod 2^{w}]
- Unsigned/signed:
- Subtraction
- Becomes an addition where negative operands are transformed into their additive inverse (in two’s complement)
- Multiplication:
- Unsigned: actual product obtained ->
(x \times y) \mod 2^{w}
- Signed: actual product obtained ->
\text{U2T}_{w}[(x \times y) \mod 2^{w}]
- Can be replaced by additions and shifts
- Unsigned: actual product obtained ->
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!