# 20155302 课堂实践

## 第二题

1. 调用附图代码，编写一个程序 “week0602学号.c",用show_int(), show_float()打印一下你的4位学号，参考教材P33打印出匹配的位序列。

2. 提交运行结果截图，要全屏，要包含自己的学号信息

## 第三题

1. 编写一个程序 “week0603学号.c",运行下面代码：

1 short int v = -学号后四位
2 unsigned short uv = (unsigned short) v
3 printf("v = %d, uv = %u\n "， v, uv);

2. 在第三行设置断点用gdb调试，用p /x v; p /x uv 查看变量的值，提交调试结果截图，要全屏，要包含自己的学号信息

## 课下作业一

2.96：

#include <stdio.h>
#include <assert.h>
#include "float-f2i.h"

int float_f2i(float_bits f) {
unsigned sig = f >> 31;
unsigned exp = f >> 23 & 0xFF;
unsigned frac = f & 0x7FFFFF;
unsigned bias = 0x7F;

int num;
unsigned E;
unsigned M;

if (exp >= 0 && exp < 0 + bias) {

num = 0;
} else if (exp >= 31 + bias) {

num = 0x80000000;
} else {
E = exp - bias;
M = frac | 0x800000;
if (E > 23) {
num = M << (E - 23);
} else {

num = M >> (23 - E);
}
}

return sig ? -num : num;
}


2.97：

#include <stdio.h>
#include <assert.h>
#include <limits.h>
#include "float-i2f.h"

int bits_length(int i) {
if ((i & INT_MIN) != 0) {
return 32;
}

unsigned u = (unsigned)i;
int length = 0;
while (u >= (1<<length)) {
length++;
}
return length;
}

return (unsigned) -1 >> (32-l);
}

float_bits float_i2f(int i) {
unsigned sig, exp, frac, rest, exp_sig /* except sig */, round_part;
unsigned bits, fbits;
unsigned bias = 0x7F;

if (i == 0) {
sig = 0;
exp = 0;
frac = 0;
return sig << 31 | exp << 23 | frac;
}
if (i == INT_MIN) {
sig = 1;
exp = bias + 31;
frac = 0;
return sig << 31 | exp << 23 | frac;
}

sig = 0;

if (i < 0) {
sig = 1;
i = -i;
}

bits = bits_length(i);
fbits = bits - 1;
exp = bias + fbits;

if (fbits <= 23) {
frac = rest << (23 - fbits);
exp_sig = exp << 23 | frac;
} else {
int offset = fbits - 23;
int round_mid = 1 << (offset - 1);

frac = rest >> offset;
exp_sig = exp << 23 | frac;

if (round_part < round_mid) {

} else if (round_part > round_mid) {
exp_sig += 1;
} else {

if ((frac & 0x1) == 1) {

exp_sig += 1;
}
}
}

return sig << 31 | exp_sig;
}


posted @ 2017-10-29 22:53  乐舞歌不是可爱送  阅读(200)  评论(0编辑  收藏  举报
Live2D