




已阅读5页,还剩95页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
2.1Perform the following number conversions:(十六进制与二进制的转换)A. 0x39A7F8 to binaryHexadecimal 3 9 A 7 F 8Binary 0011 1001 1010 0111 1111 1000B. Binary 1100100101111011 to hexadecimalBinary 1100 1001 0111 1011Hexadecimal C 9 7 BC. 0xD5E4C to binaryHexadecimal D 5 E 4 CBinary 1101 0101 1110 0100 1100D. Binary 1001101110011110110101 to hexadecimalBinary 10 0110 1110 0111 1011 0101Hexadecimal 2 6 E 7 B 52.2Fill in the blank entries in the following table, giving the decimal and hexadecimal representations of different powers of 2:n 2n (Decimal十进制) 2n (Hexadecimal十六进制)9 512 0x20019 524,288 0x8000014 16,384 0x400016 65,536 0x1000017 131,072 0x200005 32 0x207 128 0x802.3A single byte can be represented by two hexadecimal digits. Fill in the missing entries in the following table, giving the decimal, binary, and hexadecimal values of different byte patterns:(做题认真即可 进制之间的转换)Decimal Binary Hexadecimal0 0000 0000 0x00167 = 10*16 + 7 1010 0111 0xA762 = 3 * 16 + 14 0011 1110 0x3E188 = 11 * 16 + 12 1011 1100 0xBC3 * 16 + 7 = 55 0011 0111 0x378 * 16 + 8 = 136 1000 1000 0x8815 * 16 + 3 = 243 1111 0011 0xF35 * 16 + 2 = 82 0101 0010 0x5210 * 16 + 12 = 172 1010 1100 0xAC14 * 16 + 7 = 231 1110 0111 0xE72.4Without converting the numbers to decimal or binary, try to solve the following arithmetic problems, giving the answers in hexadecimal. Hint: Just modify the methods you use for performing decimal addition and subtraction to use base 16.(注意给出的数字是二进制十进制还是十六进制)十六进制得写0xA. 0x503c + 0x8 =0x5044. Adding 8 to hex c gives 4 with a carry of 1.(进位1)B. 0x503c 0x40 =0x4ffc. Subtracting 4 from 3 in the second digit position requires a borrow from the third(借位1). Since this digit is 0, we must also borrow from the fourth position.C. 0x503c + 64 =0x507c. Decimal 64 (26) equals hexadecimal 0x40.D. 0x50ea 0x503c =0xae.To subtract hex c (decimal 12) from hex a (decimal 10), we borrow 16 from the second digit, giving hex e (decimal 14). In the second digit, we now subtract 3 from hex d (decimal 13), giving hex a (decimal 10).2.5Consider the following three calls to show_bytes:(一个内存单元存放一个字节 8个比特 存储有两种方式:1小端法-最低有效字节在最前面 2大端法-最高有效字节在最前面)int val = 0x87654321;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 hexadecimal representation 0x00359141, while the floating-point number 3510593.0 has hexadecimal representation 0x4A564504.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 10000 0000 0011 0101 1001 0001 0100 00014 A 5 6 4 5 0 40100 1010 0101 0110 0100 0101 0000 0100B. Shift these two strings relative to one another to maximize the number of matching bits. How many bits match?(移动两个二进制串的相对位置,使其匹配位数最多,最多是多少位)With the second word shifted two positions to the right relative to the first, we find a sequence with 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 most significant bit having value 1. Such is the case for the example in the text as well. In addition, the floating-point number has some nonzero high-order bits that do not match those of the integer.2.7What 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 0x61 through 0x7A.It prints 61 62 63 64 65 66. Recall also that the library routine strlen does not count the terminating null character, and so show_bytes printed only through the character f.2.8Fill in the following table showing the results of evaluating Boolean operations onbit vectors. Operation Result(运算比较简单 认真 已经给了编码 字节取反即可)a 01101001b 01010101a(求反) 10010110b 10101010a & b(且) 01000001a | b(或) 01111101a b(异或) 00111100(同为0 相同为0 不同为1)2.9Computers generate color pictures on a video screen or liquid crystal display by mixing three different 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 sources R, G, and B:R G B Color0 0 0 Black0 0 1 Blue0 1 0 Green0 1 1 Cyan1 0 0 Red1 0 1 Magenta1 1 0 Yellow1 1 1 WhiteEach of these colors can be represented as a bit vector of length 3, and we can apply Boolean operations to them.A. The complement of a color is formed by turning off the lights that are on and turning on the lights 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 can see that White is the complement of Black, Yellow is the complement of Blue, Magenta is the complement of Green, and Cyan is the complement of Red.B. Describe the effect of applying Boolean operations on the following colors:(二进制的交集 或 与或运算)Blue | Green =Yellow & Cyan =Red Magenta =Blue (001) | Green (010) = Cyan (011)Yellow (110) & Cyan (011) = Green (010)Red (100) Magenta (101) = Blue (001)2.10As an application of the property that a a = 0 for any bit vector a, consider the following program: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 the locations denoted by pointer variables x and y. Note that unlike the usual technique for swapping two values, we do not need a third location to temporarily store one value while we are moving the other. There is no performance advantage to this way of swapping; it is merely an intellectual Amusement.Starting with values a and b in the locations pointed to by x and y, respectively, fill in the table that follows, giving the values stored at the two locations after each step of the procedure. Use the properties of to show that the desired effect is achieved. Recall that every element is its own additive inverse (that is, a a = 0).(交换两个数)Step *x *yInitially a bStep 1 a a bStep 2 a (a b) = (a a) b =b a bStep 3 b b (a b) = (b b) a = a2.11Armed with the function inplace_swap from Problem 2.10, you decide to write code that will reverse the elements of an array by swapping elements from opposite ends of the array, working toward the middle.(一个串倒序)You arrive at the following function:1 void reverse_array(int a, int cnt) 2 int first, last;3 for (first = 0, last = cnt-1;4 first = last;5 first+,last-)6 inplace_swap(&afirst, &alast);7 When you apply your function to an array containing elements 1, 2, 3, and 4, you find the array now 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 middle element 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 final iteration 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 *y, we get 0.We then store 0 as the middle element of the array, and the subsequent steps keep 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 swap the middle element with itself.2.12Write C expressions, in terms of variable x, for the following values. Your code should work for any word size w 8. For reference, we show the result of evaluating the expressions for x = 0x87654321, with w = 32.A. The least significant byte of x, with all other bits set to 0. 0x00000021. x & 0xFFB. All but the least significant byte of x complemented, with the least significant byte left unchanged. 0x789ABC21. x 0xFFC. The least significant byte set to all 1s, and all other bytes of x left unchanged.0x876543FF.x | 0xFF2.13The Digital Equipment VAX computer was a very popular machine from the late 1970s until the 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. They generate a result z consisting of the bits of x modified according to the bits of m. With bis, the modification involves setting z to 1 at each bit position where m is 1.With bic, the modification involves 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 and bic implementing the bit set and bit clear operations, and that we want to use these to implement functions computing bit-wise operations | and , without using any other C operations. Fill in the missing 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 x|y using only calls to functions bis and bic */int bool_or(int x, int y) int result = bis(x,y);return result;/* Compute xy 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 0x66 and 0x39, respectively. Fill in the following table indicating the byte values of the different C expressions:ExpressionValueExpressionValuex & y0x20 x & y0x01x | y0x7F x | y0x01x | y0xDF!x | !y0x00x & !y0x00x & y0x01(两个and 就是表示如果两边都为1 结果就是1 不都为1是0 如果是数表示为1 是0 即是0)把数转成 0 和1 然后再做与 或的运算 结果只有 01 和002.15Using 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 y).That is, xy will be zero if and only if every bit of x matches the corresponding bit of y.We then exploit 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 x 2(Logical) x 2(Arithmetic)Hex Binary Binary Hex Binary Hex Binary Hex0xC3 11000011 00011000 0x18 00110000 0x30 11110000 0xF00x75 01110101 10101000 0xA8 00011101 0x1D 00011101 0x1D0x87 10000111 00111000 0x38 00100001 0x21 11100001 0xE10x66 01100110 00110000 0x30 00011001 0x19 00011001 0x19 二进制 十六进制 X2 表示将数向右移2位 低位舍弃 高位0补齐 算数最地位0 用0补齐是1 用1补齐 逻辑用0补齐2.17Assuming w = 4, we can assign a numeric value to each possible hexadecimal digit, assuming either an unsigned or a twos-complement interpretation. Fill in the following table according to these interpretations by writing out the nonzero powers of two in the summations shown in Equations 2.1 and 2.3: xHexadecimal Binary B2U4(x) B2T4(x)0xE 1110 23 + 22 + 21 = 14 23 + 22 + 21=20x0 0000 0 00x5 0101 22 + 20 =5 22 + 20 = 50x8 1000 23 = 8 23=80xD 1101 23 + 22 + 20 = 13 23 + 22 + 20 =30xF 1111 23 + 22 + 21 + 20 = 15 23 + 22 + 21 + 20 =12.18In Chapter 3, we will look at listings generated by a disassembler, a program that converts an executable program file back to a more readable ASCII form. These files contain many hexadecimal numbers, typically representing values in twoscomplement form. Being able to recognize these numbers and understand their significance (for example, whether they are negative or positive) is an important skill.For the lines labeled AJ (on the right) in the following listing, convert the hexadecimal values (in 32-bit twos-complement form) shown to the right of the instruction names (sub, mov, and add) into their decimal equivalents:(把右边的十六进制的数转为十进制数,注意正负)8048337: 81 ec b8 01 00 00 sub $0x1b8,%esp A. 440804833d: 8b 55 08 mov 0x8(%ebp),%edx8048340: 83 c2 14 add $0x14,%edx B. 208048343: 8b 85 58 fe ff ff mov 0xfffffe58(%ebp),%eax C. -4248048349: 03 02 add (%edx),%eax804834b: 89 85 74 fe ff ff mov %eax,0xfffffe74(%ebp) D. -3968048351: 8b 55 08 mov 0x8(%ebp),%edx8048354: 83 c2 44 add $0x44,%edx E. 688048357: 8b 85 c8 fe ff ff mov 0xfffffec8(%ebp),%eax F. -312804835d: 89 02 mov %eax,(%edx)804835f: 8b 45 10 mov 0x10(%ebp),%eax G. 168048362: 03 45 0c add 0xc(%ebp),%eax H. 128048365: 89 85 ec fe ff ff mov %eax,0xfffffeec(%ebp) I. 276804836b: 8b 45 08 mov 0x8(%ebp),%eax804836e: 83 c0 20 add $0x20,%eax J. 328048371: 8b 00 mov (%eax),%eax2.19Using the table you filled in when solving Problem 2.17, fill in the following table describing the function T2U4:x T2U4(x) x(hex)8 8 ox83 13 oxd2 14 oxe1 15 oxf0 0 ox05 5 ox5(如果是负数 T2U4后的结果 绝对值相加为16 如果是正数 结果不变)2.20Explain how Equation 2.6 applies to the entries in the table you generated when solving Problem 2.19.For the first four entries, the values of x are negative and T2U4(x) = x + 24. For the remaining two entries, the values of x are nonnegative and T2U4(x) = x.2.21Assuming the expressions are evaluated on a 32-bit machine that uses twoscomplement arithmetic, fill in the following table describing the effect of casting and relational operations, in the style of Figure 2.18:Expression Type Evaluation-2147483647-1 = 2147483648U unsigned 1-2147483647-1 2147483647 signed 1-2147483647-1U 2147483647 unsigned 0-2147483647-1 -2147483647 signed 1-2147483647-1U -2147483647 unsigned 1(一般情况下 带U的表示不带符号 如果大小关系满足为1 不满足结果为0 )2.22Show that each of the following bit vectors is a twos-complement representation of 5 by applying Equation 2.3:A. 1011: 23 + 21 + 20 = 8 + 2 + 1 = 5B. 11011: 24 + 23 + 21 + 20 = 16 + 8 + 2 + 1 = 5C. 111011: 25 + 24 + 23 + 21 + 20 = 32 + 16 + 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 fun1(unsigned word) return (int) (word 24);int fun2(unsigned word) return (int) word 24;Assume these are executed on a machine with a 32-bit word size that uses twoscomplement arithmetic. Assume also that right shifts of signed values are performed arithmetically, while right shifts of unsigned values are performed logically.A. Fill in the following table showing the effect of these
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 模具制造数字化设计与仿真技术2025年在船舶制造行业应用分析报告
- 新能源汽车废旧电池回收利用产业链布局与发展战略研究报告
- 法制与心理健康教育
- 病患联动皮肤护理方案
- 用友U8软件操作培训课程大纲
- 煤炭安全教育培训课件
- 石墨负极材料预处理项目初步设计
- 精神科常规护理与管理优化
- 心脏骤停护理管理
- 提高健康宣教成效
- 宫腔镜手术围手术期护理
- T/CBMCA 017-2021建筑用覆膜钢板
- GB/T 20424-2025重有色金属精矿产品中有害元素的限量规范
- 腰椎穿刺课件
- 八年级物理上册《实验题》专项训练题及答案(人教版)
- 广西中医药大学赛恩斯新医药学院
- 兰州城市规划技术规定
- 【员工关系管理研究国内外文献综述2800字】
- 马家田煤矿 矿业权价款计算结果的报告
- 英语牛津3000词汇表
- GB/T 32937-2016爆炸和火灾危险场所防雷装置检测技术规范
评论
0/150
提交评论