深入理解计算机系统——课后习题

本来是不想写的,但几个月过后,再次翻阅这本书的习题时,发现都是非常基础的东西,很适合来练手

 

Chapter2:

2.61写一个C表达式,在下列描述的条件下产生1,而在其他情况下得到0。假设x是int类型。
A.x的任何位都等于1。
B.x的任何位都等于0。
C.x的最高有效字节中的位都等于1。
D.x的最低有效字节中的位都等于0。
代码应该遵循位级整数编码规则,另外还有一个限制,你不能使用相等(==)和不相等(!=)测试。

 

//P89    2.61
//x的任何位都等于1:x&0xFFFFFFFF
//x的任何位都等于0:x|0
//x的最低有效字节中的位都等于1:x&127
//x的最高有效字节中的位都等于0:x&0x00FFFFFF

 

2.62编写一个函数 int_shifts_are_arithmetic(), 若机器对int使用算术右移,返回1.否则返回0.(此题参考了https://blog.csdn.net/zhzhanp/article/details/6321503,不过感觉有点小错误,这里我更正一下)

int int_shifts_are_arithmetic(int x)
{
    int shift_val = (sizeof(int)) << 3-1;

    int xright = (-1) >> shift_val;

    return ((xright & 0x80) == 0x80);
}

2.63编写函数srl用算术右移来完成逻辑右移;函数 sra 用逻辑右移来完成算术右移。(本题参考:https://blog.csdn.net/HMSIWTV/article/details/7699650?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522160009291119725264636390%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=160009291119725264636390&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~first_rank_v2~rank_v28-1-7699650.pc_first_rank_v2_rank_v28&utm_term=%E5%B0%86%E4%B8%8B%E9%9D%A2%E7%9A%84c%E5%87%BD%E6%95%B0%E4%BB%A3%E7%A0%81%E8%A1%A5%E5%85%85%E5%AE%8C%E6%95%B4%2C%E5%87%BD%E6%95%B0srl%E7%94%A8%E7%AE%97%E6%9C%AF%E5%8F%B3%E7%A7%BB&spm=1018.2118.3001.4187)

unsigned srl(unsigned x, int k)
{
    unsigned xsra = (int)x >> k;
    int w = sizeof(int) << 3;
    return xsra |= (-1 << (w - k));
}

int sra(int x, int k)
{
    int xsrl = (unsigned)x >> k;
    int w = sizeof(int) << 3;
    return xsrl &= (-1 << (w - k));
}

 

 2.64 当x的任何奇数位等于1时返回1;反之亦然

int any_odd_one(unsigned x)
{
    return (x & 0xAAAAAAAA) != 0;
}

 2.65 ICS Return 1 when x contains an odd number of 1s,0 otherwise(判断二进制表示中1的个数是否为奇数)(该代码复制了该博客:https://blog.csdn.net/aaron_koyalun/article/details/78001832)

这道题和这个代码的主人让我感受到了异或可以用来判断字符串的奇偶性

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
 
 
 
int odd_number(unsigned x){
    x=x^(x>>1);
    x=x^(x>>2);
    x=x^(x>>4);
    x=x^(x>>8);
    x=x^(x>>16);
    
    return x&0x1;
}
 
 
int main(){
    int k;
    while(cin>>k){
        cout<<odd_number(k)<<endl;
    }
    
}

 2.69

/*
*Do rotating right shift. Assume 0<=n<=w
*Examples when x=0x12345678 and w=32:
* n=4 ->0x81234567,n=20 ->0x45678123
*/

unsigned rotate_left(unsigned x, int n)
{
    unsigned int temp = x >> (32 - n);
    return (x << n) + temp;
}

 2.70

/*Return 1 when x can be represented as an n-bit,

2's complment number;

0 otherwise*/

int fits_bits(int x, int n)
{
    int count = sizeof(int) << 3 - n;
    return ((x << count) >> count) == x;
}

 

 

//2.73
/*
Addition that saturates to TMin or TMax

参考博客:https://blog.csdn.net/Amazo/article/details/105293079

这个大佬博客写的非常详细
*/

int saturating_add(int x, int y)
{
    int result = x + y;
    int condition1 = ~(hight(x) ^ hight(y));
    int condition2 = hight(result) ^ hight(x);
    int TMin = 1 << 31;
    int result_overflow = hight(result) - TMin;
    return result_overflow & condition1 & condition2;
}

 

posted @ 2020-09-14 22:26  PYozo_free  阅读(1262)  评论(0)    收藏  举报