Introduction to Computers and Programming Lecture 5 Numeric Values 2510 using ASCII: 00110010 00110101 00000000 0000110012 Prof. I. K. Lundqvist Reading: B pp. 47-71 Sept 12 2003 25 24 23 22 21 20 32 16 8 4 2 1 1 0 1 1 0 1 32 + 0 + 8 + 4 + 0 + 1 = 45 3 7 5 1 0 1 1 One EightOne • Storing the value of • Binary notation: 1. Binary place 2. Position's quantity 3. Example binary pattern 4. Total (2. x 3.) Representation Position's quantity Representation Position's quantity HundredTenFourTwoBase ten system Base two systemFinding Binary Representation of Large Values 1. Divide the value by 2 and record the remainder 2. As long as the quotient obtained is not 0, continue to divide the newest quotient by 2 and record the remainder 3. Now that a quotient of 0 has been obtained, the binary representation of the original value consists of the remainders listed from right to left in the order they were recorded The Binary System 10 = 5x103 + 3x102 +8x101 +2x100 – 10112 = 1x23 + 0x22 + 1x21 + 1x20 = 8 + 0 + 2 + 1 = 1110 • Binary addition • A byte 0 1 0 1 0 0 1 1 7 6 5 4 3 2 1 0 1248163264128 bit number bit value – 5382 2 1 0 R1 2 3 1 R1 2 6 0 R0 2 13 6 R1 1 1 0 1 • Decimal: Position represents a power of 10 • Binary: Position represents a power of 2Representing Negative Numbers 2 = 1910. How to represent -1910 in binary? number (sign magnitude) – 000000012 = 110 100000012 = -110 – 000100112 = 1910 100100112 = -1910 Representing Negative Numbers – 000100112 = 1910 111011002 = -1910 –010: 000000002 and 111111112 add 1 – 000100112 = 1910 111011012 = -1910 – Try to negate 0: • 010 = 000000002 • invert: 000000002 Æ 111111112 • 2 + 000000012 = 000000002 representing negative numbers? add 1: 11111111 • Using one byte, any suggestions for – E.g., 00010011 • Reserve 1 bit (#7) for sign, 7 bits for • One’s complement – invert each bit • Two’s complement – invert each bit andTwo’s Complement Notation Systems Addition 3 + 2 0011 + 0010 0101 -3 +-2 1101 + 1110 1011 7 +-5 0111 + 1011 0010 5 -5 2 3 2 1 0 7 6 5 4 3 2 1 0 Problem in base ten Problem in two's complement Answer in base ten 011 Bit Pattern Bit Pattern Value Represented Value Represented 010 001 000 111 110 101 100 -1 -2 -3 -4 0111 0110 0101 0100 0011 0010 0001 0000 1111 1110 1101 1100 1011 1010 1001 -1 -2 -3 -4 -5 -6 -7 1000 -8• Example: we are using 8 bit two's complement and have the problem 97-81. 97 0 1 1 0 0 0 0 1 -81 16 0 0 0 1 0 0 0 0 Summary: One’s/Two’s Complement • Note that in sign magnitude and in one's complement there are two possible representations of 0, and we can represent every integer n with -( 2k -1) d n d 2k -1 with a k-bit field. • With two's complement there is only one representation of 0, and we can represent the integers n with -2k d n d 2k -1. • The most significant advantage of two's complement is the fact that subtraction can be performed with addition circuitry. +1 0 1 0 1 1 1 1The Problem of Overflow represented falls outside the range of values that can be represented. of two positive values results in the pattern for a negative value or vice versa. accumulate to produce large numbers. Fractions in Binary twice the size of the one to its right. 10.001 + 100.000 111.001 22 21 20 2-1 2-2 2-3 4 2 1 1/2 1/4 1/8 1 0 1 . 1 0 1 4 + 0 + 1 + 1/2+ 0 +1/8 = 5 5/8 • Overflow: when a value to be • An overflow is indicated if the addition • Remember: small values can • Each position is assigned a quantity of • 101.101 = ? 1. Binary place 2. Position's quantity 3. Example binary pattern 4. Total (2 x 3)Floating Point representation the number is divided into two parts: the integer and the fraction numbers Floating Point representation part and 4 bits to represent the fraction: 71.3425 = +1000111.0101 • To represent a floating point number – 3.1415 has integer 3 and fraction 0.1415 • Converting FP to binary: – Convert integer part to binary – Convert fraction part to binary – Put a decimal point between the two • Assume 12 bits to represent the integerNormalization point •Instead we use Normalization Move the decimal point so that there is only one 1 to the left of the decimal point. e where e is the number of bits that the decimal point moved, positive for left movement, negative for right movement Store: The sign The exponent The mantissa Excess (or bias) Notation • the exponent for floating point numbers. With 3 bits we have excess 4 (=23-1) Alternative to two’s complement used to store Bit positions ExponentSign bit 3 2 1 0 • To represent +1000111.0101 – Store sign, all bits, and the position of decimal To indicate the original value of the number, multiply by 2 Mantissa 111 Bit Pattern Value Represented 110 101 100 011 010 001 000 -1 -2 -3 -4Excess (or bias) Notation With 4 bits we have excess 8 (=24-1) With N bits we have excess 2 We add the magic number, 2N-1, to the integer, change the result to binary, and add 0’s (on the left) to make up N bits. FP Representation uses 32 bits for single-precision format: exponent (excess 127 = 28-1-1) 7 6 5 4 3 2 1 0 N-1 • The standard IEEE representation for FP –1 bit sign – 8 bits – 23 bits mantissa • Representation. Store the: – Sign as 0 (positive), 1 (negative) – Exponent (power of 2) as excess127 – Mantissa as an unsigned integer Bit Pattern Value Represented 1111 1110 1101 1100 1011 1010 1001 1000 0111 0110 0101 0100 0011 0010 0001 -1 -2 -3 -4 -5 -6 -7 0000 -8Example: FP Representation exponent (excess 16 = 2 ) 10011 1101100000 10 represents 3 is not stored, it is understood) Coding the value 2 5/8 Truncation Errors • 0 2 5/8 ExponentSign bit 1 0 . 1 0 1 1 0 1 0 1 • Consider a 16-bit representation with –1 bit sign – 5 bits 5-1 – 10 bits mantissa – The sign 0 represents positive – The exponent 10011 = 19 – The mantissa is .1101100000 (1 to left of . Mantissa Original representation Base 2 representation Raw bit pattern Lost
View Full Document