270 likes | 465 Views
Numbers and number systems. Radix number systems. Some number of positions and some number of symbols The number of positions varies by context The number of symbols is a property of the number system Decimal -- 10 symbols Binary -- 2 symbols Octal -- 8 symbols Hexadecimal -- 16 symbols.
E N D
Radix number systems • Some number of positions and some number of symbols • The number of positions varies by context • The number of symbols is a property of the number system • Decimal -- 10 symbols • Binary -- 2 symbols • Octal -- 8 symbols • Hexadecimal -- 16 symbols
Start with whole numbers • Each position has a value • Each symbol has a value • Multiply the value of the symbol by the value of the position, then add • In decimal, 3874 means • 3 times 1,000 • plus 8 time 100 • plus 7 times 10 • plus 4 times 1
Decimal, binary, octal, hex • In decimal there are 10 symbols (0..9) and the value of each position is a power of 10. • 100 = 1 = value of the units position • 101 = 10 = value of next position to the left • etc. • In binary, there are 2 symbols, 0 and 1, and the value of each position is a power of 2. • In octal, 8 symbols, and powers of 8 • In hexadecimal, 16 symbols, and powers of 16
Converting • Binary to/from octal and hex is very easy • mark off groups • Decimal is harder, but not very much. • Example • 3176 to binary, octal, hex • 71348 to decimal, binary, hex • 5A3216 to decimal, binary, octal
Include fractions • Same principles • Use negative powers of the base to the right of the radix point. (Only call it a decimal point in the decimal number system.) • 426.12810 to binary, octal, hex • 11011.10112 to decimal, octal, hex • 426.548 to decimal, binary, hex • AB1.216 to decimal, binary, octal
Negatives • Same as positive, but need a way to recognize that the value is negative. • What does negative mean? • Distance from 0, opposite direction of positive • For every positive, there is a corresponding negative. When those are added, the result is exactly 0.
Ways to represent negatives • Sign magnitude • - or ( ) or red ink or whatever • 1’s complement • reverse the bits to get the negative • (subtract from all 1’s to get the complement) • 2’s complement • subtract from a power of two 10000000 …. 000 (number of places depends on the size of the storage available.) • It so happens that reversing the bits and adding 1 accomplishes this subtraction. • excess something • Add something to the value before storing
2’s complement • For n bits, each value is expressed relative to its distance from 2n. • Technically, that means subtract each value from 2n . Conveniently, that can be accomplished by switching each bit and then adding 1 to the result. For n = 4: subtract from 24 = 10000, show only four bits. 0000 0 0100 4 1000 -8 1100 -4 0001 1 0101 5 1001 -7 1101 -3 0010 2 0110 6 1010 -6 1110 -2 0011 3 0111 7 1011 -5 1111 -1 Note, 0 comes first, then the positive values, then the negatives.
Does this work? • Check it out. • Example: 3 + 4: • 0011 + 0100 = 0111 = 7 • Example: -3 + -4: • 1101 + 1100 = 1)1001. 1001 = -7 • note that the extra 1 that flows out of the computation is not an overflow. • Example: +3 + -4: • 0011 = 1100 = 1111 = -1 Examples don’t prove correctness, of course; they are only illustrations
The point of excess something representation for values is that numbers stay in the order you expect the greatest negative is the smallest value 0 is in the middle the greatest positive is the largest value. We will see excess 127 used in representing exponents. The value 43 would be represented as 43+127 = 170 The value -43 would be represented as -43 + 127 = 84 The smaller values (the negatives) start with 0 in binary; the larger values (the positives) start with binary 1. This is the opposite of 2’s complement, where the negatives start with 1 and the positives start with 0. Excess something
0 + 0 = 0 0 + 1 = 1 Overflow: If there is not enough room to hold the result correctly. An extra bit that drops out is not necessarily an overflow. If the two numbers are of opposite signs, no overflow can occur. (Why not?) If the numbers are of the same sign, overflow occurs if the carry into the sign bit position is different from the carry out of the sign bit position. 1 + 0 = 1 1 + 1 = 0 and carry 1 Binary arithmetic - addition (Add two small negative nos.) (Result is smaller than one of them) Sign changed as a result of the computation, not because of the signs of the numbers used.
0 * 0 = 0 0 * 1 = 0 No carry. No big tables to memorize Multiplying negatives: complement, multiply the positives, set the sign appropriately. Size of the product is sum of the sizes of the multiplier and multiplicand. 1 * 0 = 0 1 * 1 = 1 Binary arithmetic - multiplication
Fractions • A part of a whole. • Approximation of the real number line, with limitations imposed by the number of places used. • Consider a decimal number expressed with not more than 5 decimal places. • How would you represent 0.0000138? Because of the limitation on the representation (only 5 places), we cannot accurately represent this number. Instead, we would use an approximation: 0.00001This is an example of a roundoff error • If we needed to represent the remaining 0.0000038 of the number, we are out of luck. This is called underflow
Overflow and Underflow Ref. Tannenbaum. Computer Organization
How to represent fractional values in binary • We have only two symbols. • We must represent • numbers: 0 and 1 • sign • radix point • How do we do that? • There have been a number of perfectly good solutions. Now that it is common to exchange data between systems, we must have one common method.
Floating point notation • By example (using 8 bit word size): • 2.5 = 10.1 in binary • One way to represent the point is to put it in the same place all the time and then not represent it explicitly at all. To do that, we must have a standard representation for a value that puts the point in the same place every time. • 10.1 = 1.01 * 21 • Use 1 bit for sign, 2 bits for exponent, rest for value • sign = 0 (positive); exponent = 01; significand = 101 • point assumed as in 1.01 • Result= 00110100
Refinement • Use 32 or 64 bits, not 8, to make more reasonable range of values • Note that the leading 1 (as in 1.01 *21) is always there, so we don’t need to waste one of our precious bits on it. Just assume it is always there. • Use excess something notation for handling negative exponents. • These ideas came from existing schemes developed for the PDP-11 and CDC 6600 computers.
IEEE Standard 754 • One way to meet the need, agreed to and accepted by most computer manufacturers. Bits 23 1 8 Single Precision Fraction Sign Exponent Double Precision Bits 52 1 11 Fraction Sign Exponent
Steps to IEEE format • Convert 35.75, for example • Convert the number to binary • 100011.11 • Normalize • 1.0001111 x 25 • Fit into the required format • 5 + 127 = 132; hide the leading 1. • 01000010000011110000000000000000 • Use Hexadecimal to make it easier to read • 420F0000
Single and double precision • Double precision uses more space, allows greater magnitude and greater precision. • Other than that, it behaves just like single precision. • We will use only single precision in examples, but any could easily be expanded to double precision.
IEEE 754 details Ref. Tannenbaum. Computer Organization
What’s normal? • Normalized means represented in the normal, or standard, notation. Some numbers do not fit into that scheme and have a separate definition. • Consider the smallest normalized value: • 1.000--000 x 2-126 • How would we represent half of that number? 1.000--000 x 2-127 But we cannot fit 127 into the exponent field 0.100 --000 x 2-126would do it. But we are stuck with that implied 1 before the implied point So, there are a lot of potentially useful values that don’t fit into the scheme. The solution: special rules when the exponent has value 0 (which represents -126).
Denormalization • This is denormalization: abandoning the “normal” scheme to exploit possibilities that would otherwise not be available. 0 Any non-zero value Sign No implied 1 before the implied point Power of two multiplier is -127
Representing Zero • How do you represent exactly 0 if there is an implied 1 in the number somewhere? • A special case of denormalized numbers, when everything is zero, the value of the number is exactly 0.0
Infinity • A special representation is reserved for infinity, because it is a useful entity to have available. 00000… …000000000 11111111 Sign
NaN (Not a Number) • One more special case reserved for undefined results: Any non-zero bit pattern 11111111 Sign