Lect. 02
Lect. 02 관련
Data Representation
Kinds of Data
- Numbers
- Integers
- unsigned
- signed
- Reals
- fixed-point
- floating-point
- Binary-coded decimal
- Integers
- Text
- ASCII Characters
- Strings
- Other
- Graphics
- Images
- Video
- Audio
Numbers are Different
- Computers use binary numbers (0's and 1's).
- Requires more digits to represent the same magnitude.
- Computers store and process numbers using a fixed number of digits (“fixed-precision”).
- Computers represent signed numbers using 2's complement instead of the more natural (for humans) “sign-plus-magnitude” representation.
Positional Number Systems
- Numeric values are represented by a sequence of digit symbols.
- Symbols represent numeric values.
- Symbols are not limited to '0'-'9'!
- Each symbol's contribution to the total value of the number is weighted according to its position in the sequence.
Polynomial Evalutaion
CONVERTING RADIX to Decimal
Note
Polynomial evaluation doesn't work if you try to convert in the other direction – i.e., from decimal to something else! Why?
Binary to Decimal Conversion
Converting to decimal, so we can use polynomial evaluation:
Variation on Polynomial Evaluation
![PROBLEMS: DECIMAL CONVERSION][1]
DECIMAL TO BINARY CONVERSION
- Converting to binary — can't use polynomial evaluation!
- Whole part and fractional parts must be handled separately!
- Whole part: Use repeated division.
- Fractional part: Use repeated multiplication.
- Combine results when finished.
DECIMAL TO BINARY CONVERSION
WHOLE PART: REPEATED DIVISION
- Divide by target radix (2 in this case)
- Remainders become digits in the new representation ()
- Digits produced in right to left order.
- Quotient is used as next dividend.
- Stop when the quotient becomes zero, but use the corresponding remainder.
EXAMPLE
operation | quotiente | remainder |
---|---|---|
48 | 1 | |
24 | 0 | |
12 | 0 | |
6 | 0 | |
3 | 0 | |
1 | 1 | |
0 | 1 |
FRACTIONAL PART: REPEATED MULTIPLICATION
- Multiply by target radix (2 in this case)
- Whole part of product becomes digit in the new representation ()
- Digits produced in left to right order.
- Fractional part of product is used as next multiplicand.
- Stop when the fractional part becomes zero (sometimes it won't).
EXAMPLE
operation | quotiente | remainder |
---|---|---|
0.2 | 0 | |
0.4 | 0 | |
0.8 | 0 | |
0.6 | 1 | |
0.2 | 1 | |
0.4 | 0 | |
HOW MUCH SHOULD WE KEEP?
- MATHEMATICIAN's answer:
- Use the proper notation:
- SCIENTIST's answer:
- Preserve significant digits and round:
- Round @ 5th digit:
- Preserve significant digits and round:
- ENGINEER's answer:
- depends on number of bits in the variable (, , , )
MORAL
Some fractional numbers have an exact representation in one number system, but not in another! E.g., has no exact representation in decimal, but does in base 3!
What about when represented in binary?
- Can these representation errors accumulate?
- What does this imply about equality comparisons of real numbers?
[PROBLEMS: REPEATED MULTIPLICATION][2]
COUNTING
- Principle is the same regardless of radix.
- Add to the least significant digit.
- If the result is less than , write it down and copy all the remaining digits on the left.
- Otherwise, write down zero and add to the next digit position, etc.
COUNTING IN BINARY
decimal | binary |
---|---|
Note
Note the pattern!
- LSB (bit 0) toggles on every count.
- Bit 1 toggles on every other count.
- Bit 2 toggles on every fourth count.
- Etc….
HEXADECIMAL NUMBERS
- The number of digit symbols is determined by the radix (e.g., )
- The value of the digit symbols range from to ( to ).
- The symbols are 0-9 followed by A-F.
- Conversion between binary and hex is trivial!
- Use as a shorthand for binary (significantly fewer digits are required for same magnitude).
MEMORIZE THIS!
hexadecimal | binary |
---|---|
BINARY/HEX CONVERSIONS
- Hex digits are in one-to-one correspondence with groups of four binary digits:
- Conversion is a simple table lookup!
- Zero-fill on left and right ends to complete the groups!
- Works because (power relationship)
PROBLEMS: DECIMAL CONVERSION
QUESTION:
Do you trust the used car salesman that tells you that the 1966 Mustang he wants to sell you has only the 13,000 miles that it's odometer shows?
- If not, what has happened?
- Why?
PROBLEM OF "FIXED PRECISION"
REPRESENTATION ROLLOVER
- Consequence of fixed precision.
- Computers use fixed precision!
- Digits are lost on the left-hand end.
- Remaining digits are still correct.
- Rollover while counting …
- Up:
- Down:
ROLLOVER IN UNSIGNED BINARY
- Consider an 8-bit byte used to represent an unsigned integer:
- Range:
- Incrementing a value of should yield , but this exceeds the range.
- Decrementing a value of should yield , but this exceeds the range.
- Exceeding the range is known as overflow.
DIFFERENCE BETWEEN ROLLOVER AND OVERFLOW
- Rollover describes a pattern sequence behavior.
- Overflow describes an arithmetic behavior.
- Whether or not rollover causes overflow depends on how the patterns are interpreted as numeric values!
- E.g., In signed two's complement representation, corresponds to counting from minus one to zero.
TWO INTERPRETATIONS
- Signed vs. unsigned is a matter of interpretation; thus a single bit pattern can represent two different values.
- Allowing both interpretations is useful:
- Some data (e.g., count, age) can never be negative, and having a greater range is useful.
WHY NOT SIGN+MAGNITUDE?
- Complicates addition :
- To add, first check the signs. If they agree, then add the magnitudes and use the same sign; else subtract the smaller from the larger and use the sign of the larger.
- How do you determine which is smaller/larger?
- Complicates comparators:
- Two zeroes!
EXAMPLE:
since
use sign of :
WHY 2'S COMPLEMENT?
- Just as easy to determine sign as in sign+magnitude.
- Almost as easy to change the sign of a number.
- Addition can proceed w/out worrying about which operand is larger.
- A single zero!
- One hardware adder works for both signed and unsigned operands.
EXAMPLE:
signed:
unsigned:
CHANGING THE SIGN
sign+magnitude:
2's complement:
EASIER HAND METHOD
- STEP 1: Copy the bits from right to left, through and including the first 1.
- STEP 2: Copy the inverse of the remaining bits.
REPRESENTATION WIDTH
You must be sure to pad the original value out to the full representation width before applying the algorithm!
CONVERTING 2's COMPLEMENT TO DECIMAL
METHOD #1
- If MSB is 0, the number is positive.
- convert as if it were unsigned.
- If MSB is 1, the number is negative.
- Apply 2's complement algorithm to find bit pattern of the corresponding positive magnitude
- Convert the bit pattern to decimal.
- Put a minus sign in front.
EXAMPLE:
METHOD #2:
Use polynomial evaluation, but make the contribution of the MSB be negative:
2's COMPLEMENT ANOMALY
Result is negative! Why?
OVERFLOW
RANGE OF UNSIGNED INTEGERS
Each of '' bits can have one of two values.
If -bits are used to represent an unsigned integer value:
- Range: to ( different values)
[PROBLEMS: UNSIGNED RANGE][4]
RANGE OF SIGNED BINARY INTEGERS
- Half of the patterns will be used for positive values, and half for negative.
- Half is .
- Positive Range: to ( patterns)
- Negative Range: to ( patterns)
EXAMPLE
for 8-Bits ()
[PROBLEMS: 2's COMPLEMENT RANGE][5]
DECIMAL ADDITION TABLE
DECIMAL ADDITION USING TABLE
BINARY ADDITION & CARRIES
0 | 0 | 0 | 0 | 0 | 0 |
0 | 0 | 1 | 1 | 0 | 1 |
0 | 1 | 0 | 1 | 0 | 1 |
0 | 1 | 1 | 2 | 1 | 0 |
1 | 0 | 0 | 1 | 0 | 1 |
1 | 0 | 1 | 2 | 1 | 0 |
1 | 1 | 0 | 2 | 1 | 0 |
1 | 1 | 1 | 3 | 1 | 1 |
- Column "" is simply the sum of , and .
- Columns & are simply the binary representation of .
BINARY ADDITION USING TABLE
BINARY SUBTRACTION & BORROWS
UNSIGNED OVERFLOW
SIGNED OVERFLOW
DETECTING OVERFLOW
[PROBLEMS: OVERFLOW][6]
COMPARING INTEGERS
FLOATING-POINT REALS
SINGLE-PRECISION FLOATING-POINT REPRESENTATION
REPRESENTATION OF CHARACTERS
CHARACTER CONSTANTS IN C
CHARACTER ESCAPES
REPRESENTATION OF STRINGS
BINARY CODED DECIMAL (BCD)
- packed (2 digits per byte):
- unpacked (1 digit per byte):