get_byte - 从x中提取指定的字节n * 字节编号: 从0 (LSB) 到 3 (MSB) * Ex: get_byte(0x12345678,1) = 0x56 */ int get_byte(int x, int n) { } /* count * bit_cnt - x二进制表示中1的个数 * Ex: bit_cnt(5) = 2, bit_cnt(7) = 3 */ int bit_cnt(int x) { } /* * is_positive - 如果 x > 0 返回1,否则返回0 * Ex: is_positive(-1) = 0. *不能直接用> */ int is_positive(int x) { } /* * neg - 返回 -x * Ex: neg(1) = -1. */ int neg(int x) { } /* * fits_bits - n位二进制补码能表示x?能返回1,否则返回0 * 1 <= n <= 32 * Ex: fits_bits(5,3) = 0, fits_bits(-4,3) = 1 */ int fits_bits(int x, int n) { } /***********************浮点数相关功能函数****************************/ */ /* * float_neg - 返回浮点数f的相反数-f(位级表示) * 当参数是 NaN是返回原参数. * 不用符号 */ float float_neg(float f) { } /* * double_float - 返回参数f的2倍值 2*f * 参数和结果都以unsigned int的形式传递, * 但他们都被解释为float的位级形式 * 当参数是 NaN是返回原参数. */ unsigned double_float(unsigned uf) { }。用c语言完成,并给出详细代码
时间: 2023-07-01 18:25:11 浏览: 55
以下是对应的代码实现:
```
/*
* get_byte - 从x中提取指定的字节n
* 字节编号:从0 (LSB) 到 3 (MSB)
* Ex: get_byte(0x12345678,1) = 0x56
*/
int get_byte(int x, int n) {
return (x >> (n << 3)) & 0xFF; // 右移位数为 n * 8,取出对应字节
}
/*
* bit_cnt - x二进制表示中1的个数
* Ex: bit_cnt(5) = 2, bit_cnt(7) = 3
*/
int bit_cnt(int x) {
int cnt = 0;
while (x) { // 遍历x的二进制位
cnt += x & 1; // 如果当前位为1,计数器加1
x >>= 1; // 右移一位
}
return cnt;
}
/*
* is_positive - 如果 x > 0 返回1,否则返回0
* Ex: is_positive(-1) = 0.
* 不能直接用>
*/
int is_positive(int x) {
return !((x >> 31) & 1) && !!x; // 判断最高位是否为1,以及x是否为0
}
/*
* neg - 返回 -x
* Ex: neg(1) = -1.
*/
int neg(int x) {
return ~x + 1; // 取反加一
}
/*
* fits_bits - n位二进制补码能表示x?能返回1,否则返回0
* 1 <= n <= 32
* Ex: fits_bits(5,3) = 0, fits_bits(-4,3) = 1
*/
int fits_bits(int x, int n) {
int mask = x >> 31; // 获取最高位的符号位
return !(((~x & mask) + (x & ~mask)) >> (n + ~0)); // 判断是否超出n位二进制补码表示范围
}
/***********************浮点数相关功能函数****************************/
/*
* float_neg - 返回浮点数f的相反数-f(位级表示)
* 当参数是 NaN 是返回原参数.
* 不用符号
*/
float float_neg(float f) {
unsigned uf = *(unsigned *)&f; // 将float类型的指针转化为unsigned类型的指针,获取位级表示
if ((uf & 0x7F800000) == 0x7F800000 && (uf & 0x7FFFFF)) // 判断是否为 NaN
return f;
else
return *(float *)&(uf ^ 0x80000000); // 取反符号位
}
/*
* double_float - 返回参数f的2倍值 2*f
* 参数和结果都以unsigned int的形式传递,
* 但他们都被解释为float的位级形式
* 当参数是 NaN 是返回原参数.
*/
unsigned double_float(unsigned uf) {
if ((uf & 0x7F800000) == 0x7F800000 && (uf & 0x7FFFFF)) // 判断是否为 NaN
return uf;
else {
int exponent = (uf >> 23) & 0xFF; // 获取指数
if (exponent == 0) // 如果指数为0
return (uf & 0x80000000) | (uf << 1); // 左移一位
else if (exponent == 0xFF) // 如果指数为全1
return uf;
else
return uf + (1 << 23); // 尾数加1
}
}
```
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)