Number Systems

advertisement
Number Systems
Purposes:
1. To understand how does a digital computer work. Binary digital computers only
work with 1’s and 0’s, or high and low voltage, or true and false.
2. To convert among different number systems. We use decimal numbers everyday.
Computers understand only binary numbers, which are lengthy and inconvenient to
human beings. Octal and Hexadecimal numbers are introduced to make both happy:
they are easier to be converted to binary numbers and also easier for us to handle.
Classification:
Unsigned
Numbers
Numbers
Double-Precision
Signed Integers
Floating-Point
Numbers
Single-Precision
Signed-Magnitude
Diminished
Radix-Complement
Radix-Complement
Unsigned Numbers
Radices and Characters:
 Binary:
 Decimal:
 Octal:
 Hexadecimal:
0, 1
0, 1, 2, 3, 4, 5, 6, 7, 8, 9
0, 1, 2, 3, 4, 5, 6, 7
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F
Structure of a number:
dn-1 dn-2 … d2 d1 d0 . d_1 d_2 … d_m
Integer Part
Fractional Part
Radix point
Note: If no fractional part, the radix point can be omitted!
Positional Notation:
N  d n 1 r n 1  d n  2 r n  2    d 1 r 1  d 0 r 0  d 1 r 1  d  2 r 2    d  m r  m ,
where d i  0,1,2, r  1, i  n  1, n  2, 2,1,0,1,2,  m, and r is the radix.
Practice Problem 1:
Given the positional notations of the following numbers: (1101.1)2, (724)8, and (BCD)16.
Converting Numbers Between Bases:
 Conversion to Decimal: write the number using the positional notation and then
perform decimal arithmetic to compute the result, which is the decimal number.
To speedup, using HORNER’s RULE.
Example: Convert (6734)8 into decimal.
Example: Convert (A6B3)16 into decimal using Horner’s rule.
Example: Convert (1101)2 into decimal.

Conversion from Decimal:
Integer part: Divide the decimal number by the base to which we want to
convert and cast out the reminders.
Fractional part: Multiply the decimal number by the base to which we want to
convert and cast out the integer part.
Rationale: based on the positional notation.
Example: Convert (29)10 into Binary, to Octal, and to Hexadecimal.
Example: Convert (217)10 into a hexadecimal, an octal and a binary number.
Example: Convert (0.57)10 to binary, octal and hexadecimal
Example: Convert (0.6328125)10 to binary, octal and hexadecimal.
Note: When converting to binary, the process may never stop for some irrational
decimal numbers. In this case, we need to (1) figure out the precision of the
decimal number; (2) determine when to stop based on the precision. Eg. (0.57)10
=0.10010001 since the precision of (0.57)10 is 1/100 and we need at least 7 bits
for this precision.

Binary to/from Octal and Hexadecimal: Starting at the binary point, cast off
three (four) bits at a time and convert each group to its octal (hexadecimal)
equivalent. Padding 0’s to the left for the integer part and to the right for the
fractional part when necessary.
Example: Convert (10110111)2 to an octal and a hexadecimal number.
Example: Convert (0.10110111)2 to an octal and a hexadecimal number
Question: How to convert octal to/from hexadecimal?
Practice Problem 2: Fill in the missing entries
Decimal
136
Binary
Octal
Hexadecimal
10101100
357
BC
23.157
0.1010110011
0.532
0
00000000
000
AF.2345
00
Signed Numbers
Signed-Magnitude: 0 for positive, 1 for negative, followed by the magnitude in
different radix. A signed-magnitude number can be represented as
n 2
N  (1  2d n 1 )  ( d i r i )
im
where d n 1 is the sign bit and d i is the ith character in the number.
Note: Generally we use floating-point numbers to represent a mixed radix-point number.
Thus we focus on signed integers now.
Example: Given the signed-magnitude notations of (+9)10 and (-9)10 in binary, octal,
decimal, and hexadecimal form.
(+9)10 = (
) 2sm and (-9)10 = (
)2sm
(+9)10 = (
) 8sm and (-9)10 = (
)8sm
(+9)10 = (
) 10sm and (-9)10 = (
)10sm
(+9)10 = (
) 15sm and (-9)10 = (
)15sm
Diminished Radix-Complement (for integer): 0 for positive, followed by the
magnitude in different radix; (r-1) for negative, followed by the complement of the
magnitude of the number:
N '  r n  N 1
where r is the radix and N is the number.
The complement is computed by taking each character in the number and subtracting the
character from (r-1).
Example (Nine’s Complement):
(+9574) 10 = (
) 9C and (-9574)10 = (
)9C
Example (One’s Complement): (+9)10 = (
)1C and (-9)10 = (
)1C What is
the result of the sum of (+9)10 and (-9)10 in one’s complement? – Negative zero
Example (15’s Complement):
(+30) 10 = (
) 15C and (-30)10 = (
)15C
Radix-Complement (for integer): For a negative number, adding one to its
Diminished-Complement; For positive numbers, radix-complement = diminished
radix-complement.
Example (Ten’s Complement):
(+9574) 10 = (
) 10C and (-9574)10 = (
)10C
Example (Two’s Complement): (+8)10 = (
)2C and (-8)10 = (
)2C What is
the result of the sum of (+8)10 and (-8)10 in one’s complement? – unique zero
Example (16’s Complement):
(+30) 10 = (
) 16C and (-30)10 = (
)16C
Fill in the entries of the following table:
Decimal
Signedmagnitude
1’s
complement
2’s
complement
notes
0
1
2
3
4
5
6
7
-7
-6
-5
-4
-3
-2
-1
Remarks:
1. The sign bit is part of the number in one’s and two’s complement notation and is
included in any computations.
2. In two’s complement , the number 0 is expressed as all zeros, the number -1 is
expressed as all 1’s.
3. Digital computers use 2’s complement for arithmetic operation.
Question: What is the largest positive/negative signed 32-bit numbers in two’s
complement?
Floating-Point Numbers
Remark:
1. We only consider binary numbers.
2. Floating-point expands the range of numbers representable in a digital computer
and improves precision.
3. It is the binary scientific notation:
Examples:
 5.42713  10 23
and
 1.1101110  2 101011
sign: +
Mantissa – one non-zero digit followed by the radix point: 5.42713
Base: 10
Exponent – signed integers: -23
IEEE 754 Single-Precision Floating-Point Standard:
IEEE 754 Double-Precision Floating-Point Standard:
Remarks:
1. The hidden 1 must be included in the significand to get the mantissa.
2. Use biased exponent to facilitate sorting. For single-precision fp, the bias is
(127)10, for double-precision, it is (1023)10.
The value of a floating-point number is:
Example: Show the IEEE 754 binary representation for the floating point number (0.75)10 in single-precision for a 32-bit computer.
Practice Problem 3: What decimal number does the following floating-point number
represent?
1100 0001 1101 0000 0000 0000 0000 0000
Special representations in IEEE 754 Standard
Single precisoin
Double precisoin
Exponent
0
0
Significand
0
Non-zero
Exponent
0
0
Significand
0
Non-zero
1-254
Anything
1-2046
anything
255
255
0
Non-zero
2047
2047
0
Non-zero
Object
0
Denomalized
numbers
Normalized
FP numbers
infinity
NaN
Overflow: The exponent of the number is too large to be put in the exponent field.
Underflow: The exponent is too small (or the absolute value is too large) to be put in the
exponent field.
Double-Precision FP increases both range and precision because there are more number
of bits in the exponent and the significand fields.
Question: What are the smallest normalized numbers (positive and negative) that a
single-precision FP can represent?
Practice Problem: Fill in the entries in the following table.
Decimal Number
0
10.5
-2/3
-0.1
Single Precision FP
Download