CMPT 295: Unit - Data Representation
Lecture 5
- Representing fractional numbers in memory
- IEEE floating point representation
Last Lecture
- 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 -> true sum may overflow its w bits in memory (annotation with diagram of unisnged overflows, see lecture 04 to see full description of diagram)
- If so, then actual sum = (equivalent to subtracting from true sum )
- Signed addition -> true sum may overflow its w bits in memory (annotation attached displaying diagram of negative and positive overflows, see Lecture 04 for detailed description of diagram)
- If so then …
- actual sum =
- true sum may be too +ve -> positive overflow OR too –ve -> negative overflow
- If so then …
- Unsigned/signed:
- Subtraction
- Becomes an addition where the 2nd operand is transformed into its additive inverse in two’s complement
- Multiplication:
- Unsigned: actual product =
- Signed: actual product =
- Can be replaced by additions and shifts
Conclusion: the same bit pattern is interpreted differently.
Questions
- Why are we learning this?
- What can we do in our program when we suspect that overflow may occur?
Demo – Looking at integer additions in C
- What does the demo illustrate?
- Unsigned addition
- Without overflow
- With overflow
- Can overflow be predicted?
- Signed addition
- Without overflow
- With positive overflow and negative overflow
- Can overflow be predicted?
- Unsigned addition
- This demo (code and results) posted on our course web site 4
Today’s Menu
- (greyed out) Representing data in memory – Most of this is review
- (greyed out) “Under the Hood” - Von Neumann architecture
- (greyed out) Bits and bytes in memory
- (greyed out) How to diagram memory -> Used in this course and other references
- (greyed out) How to represent series of bits -> In binary, in hexadecimal (conversion)
- (greyed out) What kind of information (data) do series of bits represent -> Encoding scheme
- (greyed out) Order of bytes in memory -> Endian
- (greyed out) Bit manipulation – bitwise operations
- (greyed out) Boolean algebra + Shifting
- (greyed out) Representing integral numbers in memory
- (greyed out) Unsigned and signed
- (greyed out) Converting, expanding and truncating
- (greyed out) Arithmetic operations
- (highlighted) Representing real numbers in memory
- (highlighted) IEEE floating point representation
- (greyed out) Floating point in C – casting, rounding, addition, …
We’ll illustrate what we covered today by having a demo!
Converting a fractional decimal number into a binary number (bit vector) [R2B(X)]
- How would 346.625 () be represented as a binary number?
- Expanding the subtraction method we have already seen:
Starting number: 346.625
Whole:
Value | Attempted Addition (subtraction) value | Result | Binary Implication | Note |
346 | -256 | 90 | MSb | |
90 | -128 | ☹ | ||
90 | -64 | 26 | ||
26 | -32 | ☹ | ||
26 | -16 | 10 | ||
10 | -8 | 2 | ||
2 | -4 | ☹ | ||
2 | -2 | 0 | ||
0 | -1 | ☹ | LSb |
Fractional:
Value | Attempted addition (subtraction) value | result | binary implication | notes |
.625 | -0.5 | .125 | MSb | |
.125 | -0.25 | ☹ | ||
.125 | -0.125 | 0 | LSb |
Binary representation is:
First, last binary digit before the period are MSb, LSb respectively. First, last binary digit after the period are also MSb, LSb respectively.
Negative Powers of 2:
- = 0.5
- = 0.25
- = 0.125
- = 0.0625
- = 0.03125
Converting a binary number into a fractional decimal number [R2B(X)]
- How would be represented as a fractional decimal number?
Review: Fractional decimal numbers
Positional notation:
Notation | Value |
… | … |
100 | |
10 | |
1 | |
… | … |
Example: 2.345
Digit in number | Note |
2 | |
. | |
3 | |
4 | |
5 |
Converting a binary number into a fractional decimal number [B2R(X)]
Positional notation: can this be a possible encoding scheme?
… | … |
4 | |
2 | |
1 | |
… | … |
Converting a binary number into a fractional decimal number [B2R(X)]
- How would be represented as a fractional decimal number?
- Using the positional encoding scheme:
Result: ____
Negative Powers of 2
0.5 | |
0.25 | |
0.125 | |
0.0625 | |
0.03125 | |
0.015625 | |
0.0078125 | |
0.00390625 |
Positional notation as encoding scheme?
- One way to answer this question is to investigate whether the encoding scheme allows for arithmetic operations
- Let’s see: Using the positional notation as an encoding scheme produces fractional binary numbers that can be
- added
- multiplied by 2 by shifting left
- divided by 2 by shifting right (unsigned)
Example:
Operand | Binary | Fraction | Makeup |
Divide by 2 | |||
Divide by 2 | |||
Multiply by 4 | |||
Multiply by 2 |
So far so good!
Positional notation as encoding scheme?
- Advantage (so far):
- Straightforward arithmetic: can shift to multiply and divide, convert
- Disadvantage:
- Cannot encode all fractional numbers:
- Can only represent numbers of the form (what about or -34.8)
- Only one setting of binary point within the w bits -> this limits the range of possible values
- What is this range?
- Example: w = 32 bits and binary point located at 16th bit:
- Whole number range: [0 … 65535]
- Fraction range: [0 … 1 - ε]
- Range: [0.0 … 65535.99998]
- Example: w = 32 bits and binary point located at 16th bit:
- What is this range?
- Cannot encode all fractional numbers:
Not so good anymore!
Representing fractional numbers in memory
- Here is another possible encoding scheme: IEEE floating point representation (IEEE Standard 754)
- Overview:
- Binary Numerical Form:
- s – Sign bit -> determines whether number is negative or positive
- M – Significand (or Mantissa) -> fractional part of number
- E – Exponent
- Binary Numerical Form:
- Form of bit pattern (number of items not important, focus on scale):
[s|eee|fffffffffffff]
- Most significant bit (MSb) s (similar to sign-magnitude encoding)
- exp (e) field encodes E (but is not equal to E)
- frac (f) field encodes M (but is not equal to M)
IEEE Floating Point Representation – Precision options
- Single precision: 32 bits ≈ 7 decimal digits, range:10±38 (in C: float)
- In C: diagram of memory showing:
- 1 sign bit
- 8 exp bits
- 23 frac bits
- In C: diagram of memory showing:
- Double precision: 64 bits ≈ 16 decimal digits, range:10±308 (in C: double)
- In C: diagram of memory showing:
- 1 sign bit
- 11 exp bits
- 52 frac bits
- In C: diagram of memory showing:
IEEE Floating Point Representation – Three “kinds” of values
Numerical Form:
- 1 sign bit
k
exp bits- denormalized: 00…00 (all 0’s)
- normalized: exp != 0 and exp != 11..11
- special cases: 11…11 (all 1’s)
-
n
frac bits - E = exp - bias, bias =
- M = 1 + frac
Why is E biased? Using single precision as an example:
- exp range: [00000001 .. 11111110] and bias =
- E range: [-126 .. 127]
- If no bias: E range: [1 .. 254] => to
Why adding 1 to frac? Because number V is first normalized before it is converted.
Review: Scientific Notation and normalization
- From Wikipedia:
- Scientific notation is a way of expressing numbers that are too large or too small (usually would result a long string of digits) to be conveniently written in decimal form.
- In scientific notation, nonzero numbers are written in the form
- In normalized notation, the exponent n is chosen so that the absolute value of the significand m is at least 1 but less than 10.
- Examples:
- A proton’s mass is 0.0000000000000000000000000016726 kg -> kg
- Speed of light is 299,792,458 m/s -> m/s
Syntax:
Notation | Name |
+/− | sign |
, , , … | significand |
× | times |
b | base |
exponent |
Let’s try: = ____
Summary
- Representing integral numbers (signed/unsigned) in memory:
- Encode schemes allow for small range of values exactly
- Representing fractional numbers in memory:
- Positional notation (advantages and disadvantages)
- IEEE floating point representation: wider range, mostly approximately
- Overview of IEEE Floating Point representation
- Precision options
- 3 kinds: normalized, denormalized and special values
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
18
- IEEE floating point representation
- Floating point in C – casting, rounding, addition, …