Recitation #2 15-213 Section F Spring 2006 Jernej BarbicLittle Endian to Big EndianSlide 3If-then-elseSlide 5Slide 62’s complement: general rule2’s complement: some examplesFloating pointFloating point decision diagramExampleExample #2Example #3Integer as signed int vs floatSlide 15Slide 16Good coding styleQuiz #1Recitation #215-213 Section FSpring 2006Jernej BarbicLittle Endian to Big Endian•32-bit unsigned integer:n = 37 * 224 + 121 * 216 + 80 * 28 + 178•Little Endian: •Big Endian:Little Endian to Big Endian•Little Endian: •Big Endian: unsigned int LE2BE(unsigned int x){ unsigned int result = (x << 24) | ((x << 8) & (0xFF << 16)) |((x >> 8) & (0xFF << 8)) | ((x >> 24) & 0xFF); return result;}If-then-elseif (condition) expression1;else expression2;If-then-else( condition & expression1) |(~condition & expression2)Rewrite as:if (condition) expression1;else expression2;If-then-elseif (condition) expression1;else expression2;( condition & expression1) |(~condition & expression2)Rewrite as:int abs(int x){ int sign = x >> 31; // makes either all one or all zero int neg_x = ~x + 1; // computes –x (2’s complement) * return (sign & neg_x) | (~sign & x); // if-then-else}2’s complement: general rule= -231 + b= bbb2’s complement: some examples•0x0 = 0•0x1 = 1•0x7FFFFFFF = 231-1 // largest 32-bit int•0xFFFFFFFF = -1•0xFFFFFFFE = -2•0x800000000 = -231 // smallest 32-bit int•0x800000001 = -231 + 1Floating point Single precision:Double precision:k = 11, n= 52Note: exponent boundary is NOT aligned with byte boundarye.g. 0xFF7FFFFF has lowest exponent bit zero (is normalized v.)Floating point decision diagramIs e == 0 ?x = (-1)s * 21-Bias * 0.MIs e all 1’s ?Is M == 0 ?YESNOYESIs s==0 ?+-NaNx = (-1)s * 2e-Bias * 1.MNO+YESYESNONOdenormalizednormalizedBias = 2k-1 - 1EEExample1/4 = 1.0 * 2-2s=0, e = -2 + Bias = 125, M = 0representation = 3E800000•-7/8 = -1.11 x 2-1s = 1e = -1 + Bias = 126M = 1100…0representation = 0xBF600000Example #2Example #3•-17 = -1.0001 x 24s = 1e = 4 + Bias = 131M = 000100…0representation = 0xC1880000Integer as signed int vs float•3490593 =(int) 0x00354321•3490593.0 =(float) 0x4A550C84•Any correlation in bit patterns?Integer as signed int vs float•3490593 =(int) 0x354321:•3490593 = 221 * 1.c•So, we also have 3490593.0 = 221 * 1.cHence: s=0, e=21+Bias=148, M=c00clength(c)=21Integer as signed int vs float•3490593 =(int) 0x354321:•3490593.0 =(float) 0x4A550C84:equal bitstringsGood coding style•Consistent indentation•Avoid long sequences of commands without a comment•Each source file should have an appropriate header •Have a brief comment at the beginning of each functionQuiz #1•Location: blackboard•Open book, open notes, closed computer •Available: today at 4:30pm•Duration: 30 minutes•You must start it by tomorrow 11:30pm•Covers: all material so far•Don’t close your browser while taking the quiz •If there are problems, please email
View Full Document