CSAPP第二章课后作业题

#include<stdio.h>

typedef unsigned char* byte_pointer;

int is_little_endian()
{
    int test_num = 0xff;
    byte_pointer byte_start = (byte_pointer) &test_num;

    printf("%d\n", sizeof test_num);        // 4
    printf("%d\n", sizeof byte_start[0]);   // 1

    printf("%x\n", byte_start[0]);          // ff
    printf("%x\n", byte_start[1]);          // 0
    printf("%x\n", byte_start[2]);          // 0
    printf("%x\n", byte_start[3]);          // 0

    if (byte_start[0] == 0xff) return 1;
    return 0;
}

int main()
{
    if (is_little_endian() == 1) printf("little_endian\n");  // 输出此语句
    else printf("big endian\n");
    return 0;
}

#include <stdio.h>
#include <assert.h>

// x的任何位都等于1
int A(int x)
{
    return !~x;
}

// x的任何位都等于0
int B(int x)
{
    return !x;
}

// x的最低有效字节中的位都等于1
int C(int x)
{
    return A(x | ~0xff);
}

// x的最高有效字节中的位都等于0
int D(int x)
{
    return B(x >> ((sizeof(int) - 1) << 3));
}

int main()
{
    int all_bit_one = ~0;
    int all_bit_zero = 0;

    printf("%x\n", all_bit_one);    // ffffffff
    printf("%x\n", all_bit_zero);   // 0

    assert(A(all_bit_one));
    assert(B(all_bit_zero));
    assert(C(all_bit_one));
    assert(D(all_bit_zero));

    printf("pass all assert\n");    // 程序最终输出该语句
    return 0;
}

#include <stdio.h>
#include <assert.h>

// K = 17
int A(int x)
{
    return (x << 4) + x;
}

// K = -7
int B(int x)
{
    return x - (x << 3);
}

// K = 60
int C(int x)
{
    return (x << 6) - (x << 2);
}

// K = -112
int D(int x)
{
    return (x << 4) - (x << 7);
}

int main()
{
    int x = 1958;
    assert(A(x) == 17 * x);
    assert(B(x) == -7 * x);
    assert(C(x) == 60 * x);
    assert(D(x) == -112 * x);

    printf("all assert passed");    // 程序最终输出这条语句
    return 0;
}

#include <stdio.h>
#include <assert.h>

unsigned f2u(float x)
{
    return *(unsigned*) &x;
}

int float_le(float x, float y)
{
    unsigned ux = f2u(x);
    unsigned uy = f2u(y);

    // get the sign bits
    unsigned sx = ux >> 31;
    unsigned sy = uy >> 31;

    return (ux << 1 == 0 && uy << 1 == 0) || // both zeros
            (sx && !sy) || // x < 0, y >= 0 or x <= 0, y > 0
            (!sx && !sy && ux <= uy) || // x > 0, y >= 0 or x >= 0, y > 0
            (sx && sy && ux >= uy);     // x < 0, y <= 0 or x <= 0, y < 0
}

int main()
{
    assert(float_le(-0, +0));
    assert(float_le(+0, -0));
    assert(float_le(0, 7));
    assert(float_le(-6, 0));
    assert(float_le(-5, 5));
    
    printf("all assert passed\n");
    return 0;
}

#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <time.h>

int A(int x, double dx)
{
    return (float)x == (float)dx;
}

int B(int x, double dx, int y, double dy)
{
    return dx - dy == (double)(x - y);
}

int C(double dx, double dy, double dz)
{
    return (dx + dy) + dz == dx + (dy + dz);
}

int D(double dx, double dy, double dz)
{
    return (dx * dy) * dz == dx * (dy * dz);
}

int E(double dx, double dz)
{
    return dx / dx == dz / dz;
}

int main()
{
    srand((unsigned)time(NULL));
    int x = rand();
    int y = rand();
    int z = rand();
    double dx = (double)x;
    double dy = (double)y;
    double dz = (double)z;

    printf("%d %d %d\n", x, y, z);

    assert(A(x, dx));
    printf("A passed\n");
    assert(!B(0, (double)0, INT_MIN, (double)INT_MIN));
    printf("B passed\n");
    assert(C(dx, dy, dz));
    printf("C passed\n");
    // 第一个数2^30+1, 第二个数2^23+1, 第三个数2^24+1
    assert(!D((double)0x40000001, (double)0x800001, (double)0x1000001));
    printf("D passed\n");
    assert(!E(dx, (double)0));
    printf("E passed\n");

    return 0;
}

A: 0x40490FDB
0 10000000 10010010000111111011011

0b11.0010010000111111011011


B: 22/7的二进制小数表示
0b11.001001(001)...

C:从第9位开始不同

注:以上为中科大软件学院《深入理解计算机系统》课程布置的课后作业题,老师每一章都会挑一部分题留做作业。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

青衫客36

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值