#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位开始不同
注:以上为中科大软件学院《深入理解计算机系统》课程布置的课后作业题,老师每一章都会挑一部分题留做作业。