csappHome
家庭作业记录
榆钱不知秋
请多喝水
展开
-
2.81-编写C表达式产生如下位模式,其中a^k表示符号a重复k次。
接题目:假设一个w位的数据类型。代码可以包含对参数j和k的引用,它们分别表示j和k的值,但是不能使用表示w的参数。开始作答 官方答案(已验证)这里应该是利用全1的移位A.-1 << kB.~(-1 << k) << j#include <stdio.h>#include <assert.h>/* Assume 0 <= k < w */ int A(int k) { return -1 << k;原创 2022-05-05 21:49:39 · 162 阅读 · 0 评论 -
2.80-写出函数threefourths的代码。
接题目:对于整数参数x,计算3/4x的值,向零舍人。它不会溢出。函数应该遵循位级整数编码规则。开始作答 官方答案(已验证)#include <stdio.h>#include <assert.h>#include <limits.h>/** calculate 3/4x, no overflow, round to zero ** no overflow means divide 4 first, then multiple 3, diffrent原创 2022-05-04 10:50:31 · 298 阅读 · 0 评论 -
2.79-写出函数mul3div4的代码
接题目:对于整数参数x,计算3x/4,但是要遵循位级整数编码规则。你的代码计算3x也会产生溢出。开始作答 官方答案(已验证)#include <stdio.h>#include <assert.h>#include <limits.h>/*Divide by power of 2.Assume0<=k<w-1 */int divide_power2(int x, int k) { int is_neg = x & INT_MIN原创 2022-05-04 10:44:40 · 300 阅读 · 0 评论 -
2.78-写出具有如下原型的函数的代码:
接题目:/*Divide by power of 2.Assume0<=k<w-1 */int divide_power2(int x,int k);该函数要用正确的舍入方式计算x/(2^k),并且应该遵循位级整数编码规则。开始作答 官方答案(已验证)#include <stdio.h>#include <assert.h>#include <limits.h>/*Divide by power of 2.Assume0<=k&l原创 2022-05-04 10:40:17 · 190 阅读 · 0 评论 -
2.77-假设我们有一个任务:生成一段代码,将整数变量x乘以不同的常数因子K。
接题目:为了提高效率,我们想只使用+、-和<<运算。对于下列K的值,写出执行乘法运算的C表达式,每个表达式中最多使用3个运算。A. K=17B. K=- 7C. K= 60D. K=-112开始作答 官方答案(已验证)#include <stdio.h>#include <assert.h>/* K = 17 */ int A(int x) { return (x << 4) + x; }/* K = -7 */ int B原创 2022-05-04 10:35:25 · 441 阅读 · 0 评论 -
2.76-库函数calloc有如下声明:
接题目:void *calloc(size_t nmemb, size_t size);根据库文档:“函数calloc为一个数组分配内存,该数组有nmemb 个元素,每个元素为size字节。内存设置为0。如果nmemb或size为0,则calloc返回NULL。"编写calloc的实现,通过调用malloc执行分配,调用memset将内存设置为0。你的代码应该没有任何由算术溢出引起的漏洞,且无论数据类型size_ t用多少位表示,代码都应该正常工作。作为参考,函数malloc和memset声明如下原创 2022-05-04 10:30:27 · 257 阅读 · 0 评论 -
2.75-假设我们想要计算x*y的完整的2w位表示
接题目:其中,x和y都是无符号数,并且运行在数据类型unsigned是w位的机器上。乘积的低w位能够用表达式x*y计算,所以,我们只需要一个具有下列原型的函数:unsigned unsigned_high_prod(unsigned x, unsigned y);这个函数计算无符号变量x*y的高w位。我们使用一个具有下面原型的库函数:int signed_high_prod(int x,int y);它计算在x和y采用补码形式的情况下,xy的高w位。编写代码调用这个过程,以实现用无符号数为参数原创 2022-05-04 10:21:03 · 864 阅读 · 2 评论 -
2.74-写出具有如下原型的函数的代码:
接题目:/* Determine whether arguments can be subtracted without overflow */int tsub_ok(int x,int y);如果计算x-y不溢出,这个函数就返回1。开始作答 官方答案(已验证)#include <stdio.h>#include <assert.h>#include <limits.h>/* Determine whether arguments can be su原创 2022-05-04 10:05:32 · 350 阅读 · 0 评论 -
2.73-写出具有如下原型的函数的代码:
接题目:/* Addition that saturates to TMin or TMax */int saturating_add(int x,int y);同正常的补码加法溢出的方式不同,当正溢出时,饱和加法返回TMax,负溢出时,返回TMin。饱和运算常常用在执行数字信号处理的程序中。函数应该遵循位级整数编码规则。开始作答 官方答案(已验证)#include <stdio.h>#include <assert.h>#include <limits.h原创 2022-05-04 09:54:07 · 352 阅读 · 0 评论 -
2.72-给你一个任务,写一个函数,将整数val复制到缓冲区buf中,但是只有当缓冲区中有足够可用的空间时,才执行复制。
接题目:你写的代码如下:/* Copy integer into buffer if space is available *//* WARNING: The following code is buggy */void copy_int(int val, void* buf, int maxbytes) { if (maxbytes - sizeof(val) >= 0) memcpy(buf, (void*)&val, sizeof(val));}这段代码使用了库函数m原创 2022-05-04 09:24:06 · 674 阅读 · 1 评论 -
2.71-你刚刚开始在一家公司工作,他们要实现一组过程来操作一个数据结构,要将4个有符号字节封装成一个32位unsigned。一个字中的字节从0(最低有效字节)编号到3(最高有效字节)。
分配给你的任务是:为一个使用补码运算和算术右移的机器编写一个具有如下原型的函数:接题目:/* Declaration of data type where 4 bytes are packedinto an unsigned */typedef unsigned packed_t;/* Extract byte from word. Return as signed integer */int xbyte(packed_t word, int bytenum);也就是说,函数会抽取出指定的字原创 2022-05-04 09:11:18 · 1123 阅读 · 1 评论 -
2.70-写出具有如下原型的函数的代码:
接题目:/** Return 1 when x can be represented as an n-bit, 2' s-complement* number; 0 otherwise*Assume1<=n<=w*/int fits_ bits(int x,int n);函数应该遵循位级整数编码规则。开始作答 官方答案(已验证)就是要做循环左移,这个n指的是位数。#include <stdio.h>#include <assert.h>int原创 2022-05-03 22:12:39 · 368 阅读 · 0 评论 -
2.69-写出具有如下原型的函数的代码:
接题目:/** Do rotating left shift.Assume0<=n<w* Examples when x = 0x12345678 and w = 32:*n=4 -> 0x23456781, n=20 -> 0x67812345*/unsigned rotate_left (unsigned x,int n);函数应该遵循位级整数编码规则。要注意n=0的情况。开始作答 官方答案(已验证)就是要做循环左移,这个n指的是位数。#include原创 2022-05-03 21:59:57 · 221 阅读 · 0 评论 -
2.68-写出具有如下原型的函数的代码:
接题目:/* Mask with least signficant n bits set to 1* Examples: n = 6 --> 0x3F,n = 17 --> 0x1FFFF !* Assume 1 <=n<= w*/int lower_one_mask(int n) ; 函数应该遵循位级整数编码规则。要注意n= w的情况。开始作答 官方答案(已验证)就是通过给出的n,从最低位开始连续的n个1,输出这个位级表示的十六进制值。 无符号数的算数右移。原创 2022-05-03 21:50:58 · 409 阅读 · 0 评论 -
2.67-给你一个任务,编写一个过程int_ size_ is_ 32(), 当在一个int是32位的机器上运行时,该程序产生1,而其他情况则产生0。不允许使用sizeof运算符。下面是开始时的尝试:
接题目:/* The following code does not run properly on some machines */int bad_int_size_is_32() { /* Set most significant bit (msb) of 32-bit machine */ int set_msb = 1 << 31; /* Shift past msb of 32-bit word */ int beyond_msb = 1 << 32; /*原创 2022-05-03 21:36:14 · 767 阅读 · 0 评论 -
2.66-写出代码实现如下函数
接题目/*Generate mask indicating leftmost 1 in x. Assume w =32 For example,0xFF00 -> 0x8000,and 0x6600 --> 0x4000.If x = 0.then return 0.*/int leftmost_one(unsigned x);就是要得到最高位为1的那一个比特的值官方答案(已验证)#include <stdio.h>#include <assert.h>原创 2022-05-03 20:52:07 · 455 阅读 · 0 评论 -
2.65-写出代码实现如下函数
接上题目/*Return 1 when x contains an odd bit of 1s;0 otherwise.Assume w =32 */int odd_ones(unsigned x);题目的意思是,如果x含有奇数个1,就返回1,否则就返回0。官方答案(已验证)#include <stdio.h>#include <assert.h>int odd_ones(unsigned x) { //有奇数个1就必定有奇数个0,对折异或最终得1 x ^=原创 2022-05-03 16:11:08 · 342 阅读 · 0 评论 -
2.64-写出代码实现如下函数-无符号数x至少有一奇数位为1
接题目:/*Return 1 when any odd bit of x equals 1;0 otherwise.Assume w =32 */int any_odd_one(unsigned x);开始作答题目的意思是 当无符号数x的任一奇数位都为1时,返回1,否则就返回0。原创 2022-05-03 14:39:35 · 440 阅读 · 1 评论 -
2.63-将下面的C函数代码补充完整。函数srl用算术右移(由值xsra给出)来完成逻辑右移,后面的其他操作不包括右移或者除法。
接题目:函数sra用逻辑右移(由值xsrl给出)来完成算术右移,后面的其他操作不包括右移或者除法。可以通过计算8*sizeof(int)来确定数据类型int中的位数w。位移量k的取值范围为0~w-1。unsigned srl(unsigned x, int k) { /* Perform shift arithmatically */ unsigned xsra = (int)x >> k; . . .}int sra(int x, int k) { /* Perform原创 2022-04-28 09:22:43 · 683 阅读 · 2 评论 -
2.62-编写一个函数int_shifts_are_arithmetic(),在对int类型的数使用算数右移的机器上运行时这个函数生成1,而其他情况下生成0.
接上题目你的代码应该可以运行在任何字长的机器上。在几种机器上测试你的代码。#include <stdio.h>int int_shifts_are_arithmetic() { //生成全 1 int类型数 int testnum = ~0; //最高位为1的右移,这里是保留最低有效字节的右移 int shftnum = testnum >> ((sizeof(int) - 1) << 3); return shftnum == testnum;}原创 2022-04-27 17:38:49 · 510 阅读 · 1 评论 -
2.61-写一个C表达式,在下列描述的条件下产生1,而在其他情况下得到0。假设x是int类型。
题目A:x的任何位都等于1B:x的任何位都等于0C:x的最低有效字节中的位都等于1D:x的最高有效字节中的位都等于0解答#include <stdio.h>#include <stdbool.h>bool A(int x) { bool ans = !(~x); return ans;}bool B(int x) { bool ans = !x; return ans;}bool C(int x) { bool ans = !((x &原创 2022-04-27 17:08:26 · 1934 阅读 · 1 评论 -
2.60-假设我们将一个w位的字中的字节从0(最低位)到w/8- 1(最高位)编号。写出下面C函数的代码,它会返回一个无符号值,其中参数x的字节i被替换成字节b。
接上题目:unsigned replace_byte(unsigned x, int i, unsigned char b);以下示例,说明了这个函数该如何工作:replace_byte(unsigned x, int i, unsigned char b);replace_byte(0x12345678, 2, 0xAB)-->0x12AB5678;replace_byte(0x12345678, 0, 0xAB)-- > 0x123456AB;开始作答:/** genera原创 2022-04-21 17:40:00 · 783 阅读 · 1 评论 -
2.59-编写一个C表达式,它生成一个字,由x的最低有效字节和y中剩下的字节组成。对于运算数x =0x89ABCDEF和y=0x76543210,就得到0x765432EF.
先列出表达式:(x & 0xFF) | (y & ~0xFF)#include <stdio.h>//4 bytes as example;int generate_a_word(int x, int y) { int z = 0; y = y&(~0xff); x = x&0xff; z = x|y; return z;}int main() { int x = 0x89ABCDEF; int y = 0x76543210; i原创 2022-04-21 16:32:56 · 960 阅读 · 2 评论 -
2.58-编写程序is-little-endian,当在小端法机器上编译和运行时返回1,在大端法机器上编译和运行时则返回0。这个程序应该可以运行在任何机器上,无论机器的字长是多少。
#include <stdio.h>//A byte pointer refers to a sequence of bytestypedef unsigned char * byte_pointer;//The input is a byte pointer and a byte numberbool is_little_endian(){ int depozit; short x = 29; //29 = 0x00 1d byte_pointer start =原创 2022-04-20 21:08:54 · 834 阅读 · 1 评论 -
2.57-编写程序show_short, show_long和show_double,它们分别打印类型为short, long和double的C语言对象的字节表示。请试着在几种机器上运行。
#include <stdio.h>//A byte pointer refers to a sequence of bytestypedef unsigned char * byte_pointer;//The input is a byte pointer and a byte numbervoid show_bytes(byte_pointer start, size_t len) { size_t i; for (i = 0; i < len; i++) { /原创 2022-04-20 20:20:11 · 1046 阅读 · 0 评论 -
2.56-试着用不同的示例值来运行show_bytes的代码。
代码链接:https://blog.csdn.net/weixin_45861496/article/details/124300012?spm=1001.2014.3001.5501第11行代码改为:printf("%.2x ", start[i]); //beautify39行到最后一行改为:int main() { int test = 328; //int test = 3329; //int test = 1024; test_show_bytes(test); ret原创 2022-04-20 19:46:52 · 644 阅读 · 0 评论 -
2.55-在你能够访问的不同机器上,使用show_bytes(文件show_bytes.c)编译并运行示例代码。确定这些机器使用的字节顺序。
#include <stdio.h>//A byte pointer refers to a sequence of bytestypedef unsigned char * byte_pointer;//The input is a byte pointer and a byte numbervoid show_bytes(byte_pointer start,size_t len){ size_t i; for (i = 0; i < len; i++) { p原创 2022-04-20 16:23:25 · 1405 阅读 · 1 评论