You are on page 1of 11
(NeW) et Lo 5ST (sidedah \oi+ ‘e+ + Refers to the in which data is stored in the computer. ™ Data Representation * All data stored ina computer as a sequence of bits, that is, binary digits “Vo y * This is a universal storage format for all data types, called a bit pattern + We want to learn the formats for: © Integers (for example, 1, 453, -10, 0))\ _ aN © Floating point numbers (for example, 0.009) © Characters (for example, left, Lane, a, 2A yy => Endianess (or byte-order), ~ a INTEGER REPRESENTATION ZomaS ye a= AT AEE 1 - Integers are re fixed polnt numbers with the radix point fixed after the least-significant bit. - They are contrast to real numbers or floating-point numbers. =—_ a = - Commonly-used bit-lengths for integers are 8-bit, 16-bit, 32-bit or 64-bit. @ at Schemes for integers: * Unsigned Integers: can represent zero and positive integers + Signed Integers: can represent zero, positive and negative integers. ¢€ __ sign-Magnitude representation —1's Complement representation —2's Complement representation it tation tasieomne » Integer Is of two parts

is sign bit —O means positive Examples _ 1 means negative +18 = 0 0046010 2 The 5 2 idle 18 = 10010010 Scanned wi Yih ON ey as , é Ue (Vee Sex siqsed’e7 2 * bashanes aC Mahe MaX of vngianed Wor. ‘Arve Mex of S\sihed \ (ved ey-- Dest Ges (Bearer wwe Mili of UNeidned \NteseY... \ g\ -\eF B-Ne7 Ys os ¢ Bae mig oF crashed Wess; Gemeles} p\ (A\e7 B ebe/ oe \y\ sich yo Vumbet x @ ees BD \2R Bye7 Scanned with CamScanner Example 1; Suppose that n=8 and the binary representation is 0 100 OOOIB. Sign bit is 0 = positive k Absolute value is 100 0001B = 65D Hence, the integer is +65D Example 2: Suppose that n=8 and the binary representation is 1 000 00018. Sign bit is 1 > negative Absolute value is 000 0001B = 1D Hence, the integer is -1D 1's Complement Representation magnitude of the complement (inv (hence called 1's compl he binary representation 1 000 00018 ative he complement of 000 00018, D > Hence, the integer is -126D two representations of zero: gn-magnitude: +ve: 0000 00008 , -ve: 1000 00008 © 1's complement: +ve: 0000 00008 , -ve: 1111 11118 gers and negative integers need to be processed Scanned with CamScanner 2's Complement Representation Fon negative integers, the absolute value of the integer is equal to’ ‘the magnitude of the complement of the (n-1)-bit binary pattern plus one" > (hence called 2's complement). xal ie: +18 > 00010010 Zero has only one representation -18 > 11101110 4 Example 1: Suppose that n=8 and the binary repr Sign bit is O = positive Absolute value is 100 Hence, the integer is + it her in addition and Positive and negative integers can be treated toget i subtraction. Subtraction can be carried out using the “ ‘addition logic" | > -128 = 1000 0000 Scanned with CamScanner \ovk oat lh ee ? + Represent numbers in n bits + Range from a to 2-4 we + 8 bit 2s compliment * 16 bit 28 compliment x . — -128 = 10000000 = -27 —-2') = -32768 — 27 = ONIN = 27-1 — 25-1 = +32767 — Range : -128 +127 — Range : -32768 +32767 alue lon between Twos Compl Si imal zi (a) An eight-position two: 's complement value (c) Convert decimal — 120 to binary wii ia 2" through 2°"! — 1 One ‘Take the Boolean complement ofeach bit ofthe corresponding Positive number, then add 1 to the resulting bit pattern viewed asan unsigned integer. Add additional bit positions to the left and fill in with the value ofthe original sign bit ‘ftwo numbers with the same sign (both postive or both nega tive) are added, then overflow occurs if and only if the result has te sign. Brom A, take the twos complement of B and add Scanned with CamScanner OVERFLOW OR UNDERFLOW 41. Because of the(fixed precision (ie., fixed number of bits), an n-bit 2's complement signed integer has a certain range - Overflow happens if the es not equal the car ie a — = GA+ Vp Note-> Subtraction is treated as Addition of a Positive and a Negative Integers Example: Suppose that n=8, 65D - 5D = 65D + (-51 65D 0100 0001B -5D— 111110118 + 0011 1100B — 60D pe adhd Carry i discard - OK) es compleme (Vt OR: 109 00018 — - 127D (wrong) n=8, -125D - 5D = -130D RULE: If two numbers are added, and they are both positive or both ‘overflow occurs if and only if the result has the opposite sign Carries: 1 0 70 1 111010 -80 10110000 -150 0 1101010 Scanned with CamScanner Assume you want to save this number: 99999888887777766666555554444433333 Consists of 35 digits > Tf "int" variable is initialized with this num! ” ‘al constant is too large”. As it exceeds the number of digits tha Fix: utilize small number of digits (les: (double) 99999888887777766666555554444433333.0 ey 99999888887777759E+34 = 99999888887777759000000000000000000 4 (float) 99999888887777766666555554444433333.0 9,999989E+34 = 99999890000000000000000000000000000 imber of “digits” (less precision) to (double). i! cE rat nels Ae For decimal numbers: +X Mx 10 For binary numbers: + 1.M x 2** ~ i o h base \ Vv normalized form et 0 Vf AN | As\ bie um? 0000 =+976x10" a 1976)0 = +976 x10" Suelo bybetaeneib—T at oy, viv} 2UUi9 po Scanned with CamScanner Storing floating point numbers in memory 1. FP number must be converted to binary 2. FP number must be normalized 0 positive 1> negative ‘loating-Point numbers 1. ASingle-Precision texting ag eed te Debits. a 3130 23 22 Exponent (E) | Fraction (F) 3 B 32-bit Single-Precision Floating-point Number > 1 biflo pAeftiveh negative). it it. t, bias (2**-1) = 127. dded to true exponent to be stored in exponent field 1g-point number occupies 6: 63 62_ 5251 Exponent (E) Fraction (F) $< T nD 32 Gé-bit Double-Precision Floating-point Number ~ ae A 8 Sign > 1 bit (0 positive, 1 negative). > a Scanned with CamScanner Example 1: represent the following using 32-bit floating-point format, 10100 1.101000) ae =1,1010001 x 2 r.orodon x 7" 1.101000 x 2" Solution: © 10020021 10100010000000000000000 1 10020011 10100010000000000000000 © 02101011 10100010000000000000000 1 01101011 10100010000000000000000 Exponent: 20+127 =147 -» 10010011. Exponent; 20¢127 =147 10010011. Exponent:-20+127 =107 301101011 Exponent:-20+127 =107 -901101011. Example = 2: write the representation of 7.25 ina 32 bit floating point format. Solution: Convert to binary > 111.01 Normalize > 1.1101 x2? Sign = 0 (number is positive) Exponent = 2+ 127 = 129 =10000001 ‘Mantissa = 11010000000000000000000 © 10000001 11010000000000000000000 Example ~3: write the representation of -6 , -1/32 in 32 bit floating point format. Solution: Normalized > -1.10x 2? Sign > 1 Exponent 2+127 =129 > 10000001 ‘Mantissa > 10000000000000000000000 Format: 1 10000001 10000000000000000000000 “1/32 Binary form: -0.00001 Normalized > -1.0x 2° Sign > 1 Exponent >-5127 =129 01111010 Mantissa > 00000000000000000000000 Format: 1 01111010 00000000000000000000000 = IEEE 754 FLOATING-POINT STANDARD > Single format > 32 bit = Since the mantissa is always 1.xxxxxxxxx in the normalized form, no ling 1. So, effectively: => 1 bit + 23 bits Scanned with CamScanner ONVERTING A NUMBER TO FLOATING POINT Set the sign bit- 0-++ye,1—-sve part Divide your number into two sections the integer part and the fraction P ; ogether Convert to binary - convert the two parts into binary then join them t98° with a binary point needs to be moved Work out the exponent - how many spaces the binary point vo ceo the so that it is just after the first 1 esi e the binary ‘ e in the result. If you move her is left then this number is positive, If you move it to the right then the num! negative. anne Add 127 to this number then convert to binary- <—w = - . d Format the mantissa - This is done by dropping the first 1 in the number ant recording the next 23 bits. a Al = Example: Convert decimal number 3.5 floating-point representation. a into IEEE-754 32-bit = Solution: 1. $ =0 , positive number 2.Convert 3.5 to binary = 0011.1 3.Normalize, 001.11 x 2! M=II,E=1 + 127= 128 4, Combine parts {© 10000000 1 10 0000 0000-0000 0000 0000 = 4060 O000H MAQXUr1 1 OW Suppose that IEEE-754 32-bit floating-point is BF40 0000H, find the corresponding decimal number. \ ' MED\C 24 2 \ \\Nio a\\ € representation pattern is 4 100 0000 0000 0000 0000 0000 = BF40 0000H Sign bit $= | = negative number E=OII1 1101B = 125D (biased), real E = 125-|27 = -2 “Mantissa is 1.1B (with an implicit leading 1) = | + 2! = 1.SD e =-I5 = (1 + Mantissa) « 2€ +1! x (1.5) x2? = -1.5/4 = -0.375D * sa2\6p4 \ 32420 w\e SN scanned wi jw USE BIAS IN EXPONENT? Negative expone Nts could po: uld pose a problem in comparisons With this represe, To told this. B inect comparison more difficult iased Notation is used for exponents (K + bias) If the re. al e ponent of a number is X then it is represented 45 Sign Exponent Mantissa *052001 ] 10x22 0 Found higher E: start from Left, and check each corresponding bits till eto et Rete ma oree a cee a Nr ae when comparing to +ve E. | a uses a bias of 127. Therefore, Cin t110, oni igoaooas an exponent of IEEE single-precision = -| is represented as -1 + 127 = 126 = = Ois represented as 0+ 127=127= . +L is represented as +1 + (27 = 128= 7) : wv ig found by subtracting the bias from the stored exponent Pe a ow Pe ae Poeres Negative Zero Intinity mememeeve infinity, 108111 ‘Not a Number (NaN) 1010000 Scanned with CamScanner

You might also like