CS:APP Homeworks Chapter 2

  • Chapter 2
    • 2.55
    • 2.56
    • 2.57
    • 2.58
    • 2_59
    • 2_60
    • 2.61
    • 2.62
    • 2.63

Chapter 2

2.55

点击展开代码

/* 2_55.c */
#include 

typedef unsigned char *byte_pointer;

void show_bytes(byte_pointer start, size_t len)
{
    for (size_t i = 0; i < len; i++)
        printf(" %.2x", start[i]);
    printf("\n");
}

void show_int(int x)
{
    show_bytes((byte_pointer)&x, sizeof(int));
}

void show_float(float x)
{
    show_bytes((byte_pointer)&x, sizeof(float));
}

void show_pointer(int *x)
{
    show_bytes((byte_pointer)&x, sizeof(int *));
}

void test_show_bytes(int val)
{
    int ival = val;
    float fval = (float)ival;
    int *pval = &ival;

    show_int(ival);
    show_float(fval);
    show_pointer(pval);
}

int main()
{
    int test_num;
    scanf("%d", &test_num);
    test_show_bytes(test_num);
    return 0;
}

uname -mr :

4.19.84-microsoft-standard x86_64

gcc -m64 2_55.c -o 2_55 && ./2_55 :

12345
 39 30 00 00
 00 e4 40 46
 88 7c 0d 6f fc 7f 00 00

该机器字节顺序使用小端法

2.56

啊这
\(1024\) 为例
./2.55 :

1024
 00 04 00 00
 00 00 80 44
 c8 4f 3f 59 fc 7f 00 00

2.57

点击展开代码

/* 2_57.c */
#include 

typedef unsigned char *byte_pointer;

void show_bytes(byte_pointer start, size_t len)
{
    for (size_t i = 0; i < len; i++)
        printf(" %.2x", start[i]);
    printf("\n");
}

void show_short(short x)
{
    show_bytes((byte_pointer)&x, sizeof(short));
}

void show_long(long x)
{
    show_bytes((byte_pointer)&x, sizeof(long));
}

void show_double(double x)
{
    show_bytes((byte_pointer)&x, sizeof(double));
}

void test_show_bytes(int val)
{
    short sval = (short)val;
    long lval = (long)val;
    double dval = (double)val;

    show_short(sval);
    show_long(lval);
    show_double(dval);
}

int main()
{
    int test_num;
    scanf("%d", &test_num);
    test_show_bytes(test_num);
    return 0;
}

gcc -m64 2_57.c -o 2_57 && ./2_57 :

12345
 39 30
 39 30 00 00 00 00 00 00
 00 00 00 00 80 1c c8 40

2.58

点击展开代码

/* 2_58.c */
#include 
#include 

typedef unsigned char *byte_pointer;

size_t is_little_endian()
{
    size_t test_num = 0xff;
    byte_pointer byte_start = (byte_pointer)&test_num;
    if (byte_start[0] == 0xff)
        return 1;
    return 0;
}

int main()
{
    assert(is_little_endian());
    return 0;
}

2_59

使用掩码

点击展开代码

/* 2_59.c */
#include 

int main()
{
    size_t x, y;
    size_t m = 0xff;
    scanf("%zx%zx", &x, &y);
    printf("%#zx\n", ((x & m) | (y & ~m)));
    return 0;
}

gcc -m64 2_59.c -o 2_59 && ./2_59 :

0x89ABCDEF
0x76543210
0x765432ef

2_60

点击展开代码

/* 2_60 */
#include 
#include 

unsigned replace_byte(unsigned ori, int idx, unsigned ord)
{
    if (idx < 0 || idx >= sizeof(unsigned))
        exit(-1);
    return (ori & ~(0xff << (idx << 3))) | (ord << (idx << 3));
}

int main()
{
    unsigned x, b;
    int i;
    scanf("%x%d%x", &x, &i, &b);
    printf("%#x\n", replace_byte(x, i, b));
    return 0;
}

gcc -m64 2_59.c -o 2_59 && ./2_59 :

0x12345678 2 0xAB
0x12ab5678

2.61

A : !~x
B : !x
C : !~(x | ~0xff)
D : !(x & (0xff << ((sizeof(int) - 1) << 3)))

2.62

点击展开代码

/* 2_62.c */
#include 
#include 

int int_shifts_are_arithmetic()
{
    int num = -1;
    return !(num ^ (num >> 1));
}

int main()
{
    assert(int_shifts_are_arithmetic());
    return 0;
}

2.63

点击展开代码

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

int sra(int x, int k)
{
    int xsrl = (unsigned)x >> k;
    int w = sizeof(int) << 3;
    int mask = (int)-1 << (w - k);
    mask &= !(x & (1 << (w - 1))) - 1; 
    // x 符号位是 1 ,则 mask&(int)-1 ,结果不变;若符号位是 0 ,则按位与 0 ,mask 变成 0
    return xsrl | mask;
}

你可能感兴趣的:(CS:APP Homeworks Chapter 2)