【发布时间】:2012-09-17 20:54:13
【问题描述】:
这是一个家庭作业问题,我完全没有想法(C 编程)。
说明:
/*
* float_neg - Return bit-level equivalent of expression -f for
* floating point argument f.
* Both the argument and result are passed as unsigned int's, but
* they are to be interpreted as the bit-level representations of
* single-precision floating point values.
* When argument is NaN, return argument.
* Legal ops: Any integer/unsigned operations incl. ||, &&. also if, while
* Max ops: 10
* Rating: 2
*/
unsigned float_neg(unsigned uf) {
return 2;
我试过简单地返回 -uf;和其他事情,但我只是不知道该怎么做。
帮助?
可以假设以下条件:
- 使用 2s 补码,32 位整数表示。
- 以算术方式执行右移。
- 将整数移动更多时会出现不可预知的行为 大于字长。
-编辑
解决了:返回uf^0x80000000;
【问题讨论】:
-
查看en.wikipedia.org/wiki/Single-precision_floating-point_format - 你需要翻转输入的最高位并返回结果,除非输入是NaN
-
如果那条评论是你的老师写的,那肯定会令人困惑。
-
值得注意的是,这个赋值定义的“否定”与 IEEE-754 标准相反,该标准规定否定应该翻转所有输入的编码符号位,包括 NaN(第 5.5.5 节)。 1 在 754-2008)。
标签: c floating-point negate