第1关:float_neg
任务描述
本关任务:补充函数float_neg()
,返回-uf
的位级表示。
- 操作符使用数量限制:
10
注意:
- 本题及以下所有的题目都采用
unsigned int
来存放位级表示 - 所有的浮点类型都为
float
- 如果输入为
NaN
,返回NaN
测试说明
平台会对你编写的代码进行测试:
测试输入:-111
预期输出:0xffffff91
测试输入:-12345
预期输出:0xffffcfc7
int bitAnd(int x, int y)
{ return 0;
}int getByte(int x, int n)
{ return 0;
}int logicalShift(int x, int n)
{return 0;
}int bitCount(int x)
{return 0;
}int bang(int x)
{return 0;
}int tmin(void)
{ return 0;
}int fitsBits(int x, int n)
{return 0;
}int divpwr2(int x, int n)
{return 0;
}int negate(int x)
{return 0;
}int isPositive(int x)
{return 0;
}int isLessOrEqual(int x, int y)
{return 0;
}int ilog2(int x)
{return 0;
}unsigned float_neg(unsigned uf)
{/********* Begin *********/unsigned r=uf;unsigned t;//符号取反r = r^0x80000000 ;t = (uf>>23)&0xff;//非数if (t == 0xFF && (uf&0x7FFFFF)>0)r = uf;return r;/********* End *********/
}unsigned float_i2f(int x)
{return 0;
}unsigned float_twice(unsigned uf)
{return 0;
}
第2关:float_i2f
任务描述
本关任务:补充函数float_i2f()
,实现由 int
到 float
的类型转换。
- 操作符使用数量限制:
30
测试说明
平台会对你编写的代码进行测试:
测试输入:3510593
预期输出:0x4a564504
int bitAnd(int x, int y)
{ return 0;
}int getByte(int x, int n)
{ return 0;
}int logicalShift(int x, int n)
{return 0;
}int bitCount(int x)
{return 0;
}int bang(int x)
{return 0;
}int tmin(void)
{ return 0;
}int fitsBits(int x, int n)
{return 0;
}int divpwr2(int x, int n)
{return 0;
}int negate(int x)
{return 0;
}int isPositive(int x)
{return 0;
}int isLessOrEqual(int x, int y)
{return 0;
}int ilog2(int x)
{return 0;
}unsigned float_neg(unsigned uf)
{return 0;
}unsigned float_i2f(int x)
{/********* Begin *********/unsigned abs = x;unsigned s = 0x80000000 & x;unsigned tem = 0x40000000;unsigned exp_sign = 0;unsigned exp = 0;unsigned frac;unsigned c = 0;if (x == 0)return x;else if (x == 0x80000000)return (s + (158 << 23));else {if (x < 0)abs = -x;while (1) {if (tem & abs)break;tem = tem >> 1;exp_sign = exp_sign + 1;}frac = (tem - 1) & abs;if (31 - 1 - exp_sign > 23) {int i = 30 - exp_sign - 23;if ((frac << (31 - (i - 1))) == 0x80000000) {if ((frac & (1 << i)) != 0)c = 1;} else if ((frac & (1 << (i - 1))) != 0)c = 1;frac = frac >> i;} elsefrac = frac << (23 - (31 - exp_sign - 1));exp = 157 - exp_sign;return (s + (exp << 23) + frac + c);}/********* End *********/
}unsigned float_twice(unsigned uf)
{return 0;
}
第3关:float_twice
任务描述
本关任务:补充函数float_twice()
,返回2*f
的位级表示。
- 操作符使用数量限制:
30
测试说明
平台会对你编写的代码进行测试:
测试输入:111
预期输出:0xde
int bitAnd(int x, int y)
{ return 0;
}int getByte(int x, int n)
{ return 0;
}int logicalShift(int x, int n)
{return 0;
}int bitCount(int x)
{return 0;
}int bang(int x)
{return 0;
}int tmin(void)
{ return 0;
}int fitsBits(int x, int n)
{return 0;
}int divpwr2(int x, int n)
{return 0;
}int negate(int x)
{return 0;
}int isPositive(int x)
{return 0;
}int isLessOrEqual(int x, int y)
{return 0;
}int ilog2(int x)
{return 0;
}unsigned float_neg(unsigned uf)
{return 0;
}unsigned float_i2f(int x)
{return 0;
}unsigned float_twice(unsigned uf)
{/********* Begin *********/unsigned int exp = 0x7f800000 & uf;unsigned int frac = 0x007fffff & uf;unsigned int s = 0x80000000 & uf;if (((~exp) & 0x7f800000) == 0) {return uf;} else {if (exp == 0) {if ((0x00400000 & uf) == 0) {frac = frac << 1;} else {exp = 0x00800000;frac = frac << 1;}} else {exp = exp + 0x00800000;if (((~exp) & 0x7f800000) == 0)frac = 0;}return (exp | frac | s);}/********* End *********/
}