CSAPP 第2章 信息表示和处理课后作业

您所在的位置:网站首页 整数int最高位 CSAPP 第2章 信息表示和处理课后作业

CSAPP 第2章 信息表示和处理课后作业

2024-07-10 13:58| 来源: 网络整理| 查看: 265

CSAPP 第二章信息表示和处理 课后作业

2.60

假设我们将一个 w 位的字中的字节从 0 (最低位) 到 w/8-1 (最高位) 编号。写出下面 C 函数的代码,它会返回一个无符号值,

其中参数 x 的字节 i 被替换成字节 b:

unsigned replace_byte(unsigned x, int i, unsigned char b);

以下示例,说明了这个函数该如何工作:

replace_byte(0x12345678, 2, 0xAB) --> 0x12AB5678

replace_byte(0x12345678, 0, 0xAB) --> 0x123456AB

#include unsigned replace_byte(unsigned x, int i, unsigned char b) { // Shift Amount i * 8 int shift_amount = i > 4); x = x | (x >> 8); x = x | (x >> 16); return (x & ((~x >> 1) | 0x80000000)) }

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_is32() { /* Set most significant bit (msb) of 32-bit machine */ int set_msb = 1


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3