云南大学软件学院_大二年级上册学期__专业英语重点及习题总结.pdf
2.1 Perform the following number conversions 十六进制与二进制的转换)A.0 x39A7F8 to binaryHexadecimal 3 9 A 7 F 8Binary 0011 1001 1010 0111 1111 1000B.Binary 1100100101111011 to hexadecimalBinary1100100101111011HexadecimalC97BC.0 xD5E4C to binaryHexadecimalD5E4cBinary11010101111001001100D.Binary 1001101110011110110101 to hexadecimalBinary 10 0110 1110 0111 1011 0101Hexadecimal 2 6 E 7 B 52.2 Fill in the blank entries in the following table,giving the decimal and hexadecimalrepresentations of different powers of 2:n 2An(Decimal 十进制)2、(Hexadecimal 十六进制)95120 x20019524,2880 x800001416,3840 x40001665,5360 x1000017131,0720 x200005320 x2071280 x802.3A single byte can be represented by two hexadecimal digits.Fill in the missing entries in thefollowing table,giving the decimal,binary,and hexadecimal values of different byte patterns:(做题认真即可进制之间的转换)DecimalBinaryHexadecimal00000 00000 x00167=10*16+7101001110 xA762=3*16+140011 11100 x3E188=11*16+121011 1100OxBC3*16+7=550011 01110 x378*16+8=1361000 10000 x8815*16+3=243mi ooii0 xF35*16+2=820101 00100 x5210*16+12=1721010 1100OxAC14*16+7=231111001110 xE72.4 Without converting the numbers to decimal or binary,try to solve the following arithmeticproblems,giving the answers in hexadecimal.Hint:Just modify the methods you use forperforming decimal addition and subtraction to use base 16.(注意给出的数字是.进制卜进制还是十六进制)十六进制得写OxA.0 x503c+0 x8=0 x5044.Adding 8 to hex c gives 4 with a carry of 1.(进位 1)B.0 x503c-0 x40=0 x4ffc.Subtracting 4 from 3 in the second digit position requires a borrowfrom the third(借位 1).Since this digit is 0,we must also borrow from the fourth position.C.0 x503c+64=0 x507c.Decimal 64(26)equals hexadecimal 0 x40.D.0 x50ea-0 x503c=0 xae.To subtract hex c(decimal 12)from hex a(decimal 10),we borrow16 from the second digit,giving hex e(decimal 14).In the second digit,we now subtract 3 fromhex d(decimal 13),giving hex a(decimal 10).2.5Consider the following three calls to show_bytes:(个内存单元存放,个 工行 8 个比特存储有两种方式:1 小端法一-最低有效字节在最前面2 大端法最高有效字节在最前面)继续我们前面的示例,假设变鼠X类型为in i.位于地址0 x100处,有一个十六进制值为0 x01234567o地址范围0 x100 0 x103的字节顺序依赖于机器的类型:大端法0 x1000 x1010 x1020 x10310 1231 4567|.小端法0 x1000 x1010 x1020 x10316 71451 2301 1 注意,在字0 x01234567中,高位字节的十六进制值为0 x 0 1,而低位字节值为0 x67。个字节指针引用一个字节序列,其中每个字节都被认为是一个非负整数。第一个例程show_bytes的输入是个字节序列的地址(它用一个字节指针来指示)和一个字节数。showj)ytes打印出以十六进制表小的字节。C格式化指令.2x”表示整数必须用至少两个数字的十六进制格式输出。int val=0 x87654321;byte_pointer valp=(byte_pointer)&val;show_bytes(valp,1);/*A.*/show_bytes(valp,2);/*B.*/show_bytes(valp,3);/*C.*/Indicate which of the following values will be printed by each call on a littleendianmachine and on a big-endian machine:(小端:从低到高 大端:从高到低)A.Little endian:21 Big endian:87B.Little endian:21 43 Big endian:87 65C.Little endian:21 43 65 Big endian:87 65 432.6Using show_int and show_float,we determine that the integer 3510593 has hexadecimalrepresentation 0 x00359141,while the floating-point number 3510593.0 has hexadecimalrepresentation 0 x4A564504.A.Write the binary representations of these two hexadecimal values.(用二进制表不这两个卜六进制)Using the notation of the example in the text,we write the two strings as follows:0 0 3 5 9 1 4 100(X)0(X)0 0011 0101 1001 0001 0100 00014 A 5 6 4 5 0 40100 10100101 01100100 0101 0000 0100B.Shift these two strings relative to one another to maximize the number of matching bits.Howmany bits match?(移动两个二进制串的相对位置,使其匹配位数最多,最多是多少位)With the second word shifted two positions to the right relative to the first,we find a sequencewith 21 matching bits.C.What parts of the strings do not match?We find all bits of the integer embedded in the floating-point number,except for the mostsignificant bit having value 1.Such is the case for the example in the text as well.In addition,thefloating-point number has some nonzero high-order bits that do not match those of the integer.c.税们发现除了累有效佳1,搂数的所有他域在浮点数中.这正好也是书中示例的情况,另外,“就有一些解的雕蒲整数中的雕相匹配.2.7 What would be printed as a result of the following call to show_bytes?(show.bytes返回指针 指向使用字节的最低字节地址)const char*s=abcdef”;show_bytes(byte_pointer)s,strlen(s);Note that letters a through z have ASCII codes 0 x61 through 0 x7A.It prints 61 62 63 64 65 66.Recall also that the library routine strlen does not count theterminating null character,and so show_bytes printed only through the character f .2.8 Fill in the following table showing the results of evaluating Boolean operations onbit vectors.Operationab a(求反)ba&b(且)a I b(或)aAb(异或)Result(运算比较简单 认真 已经给了编码 字节取反即可)01101(X)1010101011001011010101010010000010111110100111100(同 为()相 同 为0不 同 为1)2.9 Computers generate color pictures on a video screen or liquid crystal display by mixing threedifferent colors of light:red,green,and blue.Imagine a simple scheme,with three different lights,each of which can be turned on or off,projecting onto a glass screen.计算机生成彩色照片视频屏幕或液晶显示器通过混合三种不同颜色的光线:红、绿、蓝。想象个简单的计划,三个不同的灯光,每 种都可以打开或关闭,投射在 个玻璃屏幕。We can then create eight different colors based on the absence(0)or presence(1)of light sourcesR,G,and B:RGBColor000Black001Blue010Green011Cyan100Red101Magenta110Yellow111WhiteEach of these colors can be represented as a bit vector of length 3,and we can apply Booleanoperations to them.A.The complement of a color is formed by turning off the lights that are on and turning on thelights that are off.What would be the complement of each of the eight colors listed above?Colors are complemented by complementing the values of R,G and B.(取补)From this,we cansee that White is the complement of Black,Yellow is the complement of Blue,Magenta is thecomplement of Green,and Cyan is the complement of Red.B.Describe the effect of applying Boolean operations on the following colors:(:进 制 的 交 集 或 勺 或 运 算)Blue I Green=Yellow&Cyan=Red A Magenta=Blue(001)I Green(010)=Cyan(Oil)Yellow(110)&Cyan(011)=Green(010)Red(100)A Magenta(101)=Blue(001)2.10As an application of the property that a A a=0 for any bit vector a,consider the followingprogram:1 void inplace_swap(int*x,int*y)2*y=*x 八*y;/*Step 1 */3*x=*x 八 *y;/*Step 2*/4*y=*x 八 *y;/*Step 3*/5 As the name implies,we claim that the effect of this procedure is to swap the values stored at thelocations denoted by pointer variables x and y.Note that unlike the usual technique for swappingtwo values,we do not need a third location to temporarily store one value while we are movingthe other.There is no performance advantage to this way of swapping;it is merely an intellectualAmusement.Starting with values a and b in the locations pointed to by x and y,respectively,fill in the table thatfollows,giving the values stored at the two locations after each step of the procedure.Use theproperties of A to show that the desired effect is achieved.Recall that every element is its ownadditive inverse(that is,a A a=0).(交换两个数)Step*x*yInitially a bStep 1 a aA bStep 2 a A(a A b)=(a A a)A b=b aA bStep 3 b bA(a A b)=(b A b)A a=a2.11 Armed with the function inplace_swap from Problem 2.10,you decide to write code that willreverse the elements of an array by swapping elements from opposite ends of the array,workingtoward the middle.(一个串倒序)You arrive at the following function:1 void reverse_array(int a,int ent)2 int first,last;3 for(first=0,last=cnt-1;4 first=last;5 first+,last)6 inplace_swap(&afirst,&alast);7When you apply your function to an array containing elements 1,2,3,and 4,you find the arraynow has,as expected,elements 4,3,2,and 1.When you try it on an array with elements 1,2,3,4,and 5,however,you are surprised to see that the array now has elements 5,4,0,2,and 1.In fact,you discover that the code always works correctly on arrays of even length,but it sets the middleelement to 0 whenever the array has odd length.A.For an array of odd length cnt=2k+1,what are the values of variables first and last in the finaliteration of function reverse_array?Both first and last have value k,so we are attempting to swap the middle element with itself.B.Why does this call to function xor_swap set the array element to 0?In this case,arguments x and y to inplace_swap both point to the same location.When we compute*x A*y,we get O.We then store 0 as the middle element of the array,and the subsequent stepskeep setting this element to 0.C.What simple modification to the code for reverse_array would eliminate this problem?Simply replace the test in line 4 of reverse_array to be first last,since there is no need to swapthe middle element with itself.2.12Write C expressions,in terms of variable x,for the following values.Your code should workfor any word size w 2 8.For reference,we show the result of evaluating the expressions for x=0 x87654321,with w=32.A.The least significant byte of x,with all other bits set to 0.0 x(X)000021.X&OxFFB.All but the least significant byte of x complemented,with the least significant byte leftunchanged.0 x789ABC21.X A 0 xFFC.The least significant byte set to all 1 s,and all other bytes of x left unchanged.0 x876543FF.x IOxFF2.13The Digital Equipment VAX computer was a very popular machine from the late 1970s untilthe late 1980s.Rather than instructions for Boolean operations And and Or,it had instructions bis(bit set)and bic(bit clear).Both instructions take a data word x and a mask word m.Theygenerate a result z consisting of the bits of x modified according to the bits of m.With bis,themodification involves setting z to 1 at each bit position where m is l.With bic,the modificationinvolves setting z to 0 at each bit position where m is 1.To see how these operations relate to the C bit-level operations,assume we have functions bis andbic implementing the bit set and bit clear operations,and that we want to use these to implementfunctions computing bit-wise operations I and A,without using any other C operations.Fill in themissing code below.Hint:Write C expressions for the operations bis and bic./*Declarations of functions implementing operations bis and bic*/int bis(int x,int m);int bic(int x,int m);/*Compute xly using only calls to functions bis and bic*/int bool_or(int x,int y)int result=bis(x,y);return result;)/*Compute xAy using only calls to functions bis and bic*/int bool_xor(int x,int y)int result=bis(bic(x,y),bic(y,x);return result;)2.14Suppose that x and y have byte values 0 x66 and 0 x39,respectively.Fill in the following tableindicating the byte values of the different C expressions:ExpressionValueExpressionValuex&y0 x20 x&y0 x01x 1 y0 x7Fxlly0 x01 x 1 yOxDF!x II!y0 x00 x&!y0 x00 x&y0 x01(两个an d 就是表示如果两边都为1 结果就是1不都为1是 0如果是数表示为1是 0即是0)把数转成0 和 1然后再做与 或的运算 结果只有01 和 002.15 Using only bit-level and logical operations,write a C expression that is equivalent to x=y.In other words,it will return 1 when x and y are equal,and 0 otherwise.The expression is!(X A y).That is,xAy will be zero if and only if every bit of x matches the corresponding bit of y.We thenexploit the ability of!to determine whether a word contains any nonzero bit.2.16 Fill in the table below showing the effects of the different shift operations on single bytequantities.The best way to think about shift operations is to work with binary representations.Convert the initial values to binary,perform the shifts,and then convert back to hexadecimal.Each of the answers should be 8 binary digits or 2 hexadecimal digits.二进制 十六进制X 3 表示讲数像左移3 位 高位舍弃 低 位 0 补齐 X 2 表示将数向右移2 位 低位舍弃 高位0 补齐 算数最地位0 用 0 补齐是1 用 1 补齐逻辑用0 补齐Xx 3x 2(Logical)x 2(Arithmetic)HexBinaryBinaryHexBinary 1 HexBinaryHex0 xC3110000111000110000 x18001100000 x3011110000OxFO0 x7501110101101010000 xA800011101Ox ID0(X)11101OxlD0 x87100001111001110000 x38001000010 x2111100001OxEl0 x6601100110001100000 x30000110010 x19000110010 x192.17Assuming w=4,we can assign a numeric value to each possible hexadecimal digit,assumingeither an unsigned or a two s-complement interpretation.Fill in the following table according tothese interpretations by writing out the nonzero powers of two in the summations shown inEquations 2.1 and 2.3:HexadecimalBinaryB2U4(x)B2T4(x)OxE1H02八 3+2八 2+2八 1 =14-2八 3+2八 2+2八 1二-20 x00000000 x501012A2+2A0=52A2+2A0=50 x810002八 3 二 8-2A3=-8OxD11012A3+2A2+2A0=13-2A3+2A2+2A0=-3OxF1H12八 3+2八 2+2八 1 +20=15-2八 3+2八 2+2八 1 +2八 0=-1 无符号的二进制”,Tw-1-732*1+2七2 B2Uw(x)=2,1=0i=02.18In Chapter 3,we will look at listings generated by a disassembler,a program that converts anexecutable program file back to a more readable ASCII form.These files contain manyhexadecimal numbers,typically representing values in two scomplement form.Being able torecognize these numbers and understand their significance(for example,whether they arenegative or positive)is an important skill.For the lines labeled A-J(on the right)in the following listing,convert the hexadecimal values(in 32-bit two,s-complement form)shown to the right of the instruction names(sub,mov,and add)into their decimal equivalents:(把右边的十六进制的数转为十进制数,注意正负)8048337:81 ec b8 0100 00sub$0 xlb8,%espA.440804833d:8b 55 08mov0 x8(%ebp),%edx8048340:83 c2 14add$0 xl4,%edxB.208048343:8b 85 58 feffffmov0 xfffffe58(%ebp),%eaxC.-4248048349:03 02add(%edx),%eax804834b:89 85 74 fe ff ffmov%eax,0 xfffffe74(%ebp)D.-3968048351:8b 55 08mov0 x8(%ebp),%edx8048354:83 c2 44add$0 x44,%edxE.688048357:8b85c8feffffmov0 xfffffec8(%ebp),%eaxF.-312804835d:89 02mov%eax,(%edx)804835f:8b 45 10mov0 x_10(%ebp),%eaxG168048362:03 45 0cadd0 xc(%ebp),%eaxH.128048365:89 85 ec fe ff ffmov%eax,0 x fffffeec(%ebp)I.276804836b:8b 45 08mov0 x8(%ebp),%eax804836e:83 cO 20add$0 x20,%eaxJ.328048371:8b 00mov(%eax),%eax2.19 Using the table you filled in when solving Problem 2.17,fill in the following table describingthe function T2U4:XT2U4(x)x(hex)-88ox8-313oxd-214oxe-115oxf00oxO55ox5函数72Ux)士 8 2 UW(T 2 8W(X)生成一个无符号数,(如果是负数T2U4后的结果绝对值相加为1 6如果是正数结果不变)B2UT2BW(*)=T2UW(x)=几0+x叫(X)=,x+2*.x02.20 Explain how Equation 2.6 applies to the entries in the table you generated when solvingProblem 2.19.For the first four entries,the values of x are negative and T2U4(x)=x+2A4.For theremaining two entries,the values of x are nonnegative and T2U4(x)=x.2.21 Assuming the expressions are evaluated on a 32-bit machine that uses two scomplementarithmetic,fill in the following table describing the effect of casting and relational operations,inthe style of Figure 2.18:(一般情况下 带 U 的表示不带符号如果大小关系满足为1不满足结果为0)ExpressionTypeEvaluation-2147483647-1=2 1 47483648Uunsigned1-2147483647-1 2147483647signed1-2147483647-1U 2147483647unsigned0-2147483647-1-2147483647signed1-2147483647-1U -2147483647unsigned12.22S how that each of the following bit vectors is a twos-complement representation of-5 byapplying Equation 2.3:A.1011:-2A3+2A1 +2A0=-8 +2+1 =-5B.11011:-2八 4+2人 3+2八 1+2八 0=-1 6 +8+2+1=-5C.111011:-2八 5+2八 4+2八 3+2人 1 +2八 0=-3 2+1 6+8 +2+1=-5Observe that the second and third bit vectors can be derived from the first by sign Extension.2.23Consider the following C functions:int fun 1 (unsigned word)return(int)(word 24)24);)int fun2(unsigned word)return(int)word 24)24;)Assume these are executed on a machine with a 32-bit word size that uses two scomplementarithmetic.Assume also that right shifts of signed values are performed arithmetically,while rightshifts of unsigned values are perform