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)
-
Same thing happens when we add two binary numbers:
Unsigned addition (limited space, i.e., fixed size in memory)
What happens when we add two unsigned values?
a)
Binary:
Decimal:
b)
Binary:
Decimal:
Unsigned addition () 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
- Operands: w bits
- True Sum: w+1 bits
Closer look at unsigned addition overflow
- w = 8 -> [0..255]
Example 1:
Decimal (carry out the 1 in 135):
Binary (carry out the 1 in ):
- True sum, w=8:
- Actual (overflowed) sum, w=8:
Example 2:
Decimal (carry 1 to the 3 in 300):
Binary (carry the 1 at the beginning of the result):
- True sum, w=9:
- Actual (overflowed) sum, w=8:
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 () + 15 () = 30 () as a true sum, and 14 () 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:
Decimal:
b)
Binary:
Decimal:
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 () 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
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]
Example 1:
Decimal:
Binary:
The binary result is -121
Example 2:
Decimal:
Binary:
Binary result is 121
Example 3:
Decimal:
Binary:
Example 4:
Decimal:
Binary:
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 () + 1 () = 8 () is the true sum and = -8 () 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:
becomes:
- Let‘s try:
Decimal:
Binary:
Binary subtraction by addition:
Binary subtraction by addition is equal to -11
Check:
T2B(X) conversion:
Multiplication () 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
-
Discard: w bits
- Discarding high order w bits has same effect as applying modular arithmetic
- Example: w = 4
Decimal:
Binary:
Multiplication with power-of-2 versus shifting
- If where then
x << k
- For both signed and unsigned
- Example:
- =
x << 3
- = (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 =>
- 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 =>
- Unsigned/signed:
- Subtraction
- Becomes an addition where negative operands are transformed into their additive inverse (in two’s complement)
- Multiplication:
- Unsigned: actual product obtained ->
- 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!