编写代码 neg - 返回 -x
时间: 2024-02-11 08:08:46 浏览: 34
可以使用按位取反再加1的方法来实现-x的计算,具体实现如下:
```c
int neg(int x) {
return ~x + 1;
}
```
这里的~x表示对x进行按位取反(即0变为1,1变为0),然后再加1即可得到-x的值。
相关问题
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语言完成,并给出详细代码
以下是对应的代码实现:
```
/*
* 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
}
}
```
Define the stream non-neg-integers. Show how to define the series: S1 = 1 + x + x2 + x3 + … S2 = 1 + 2x + 3x2 + 4x3 + … Turn in your definitions and a couple of coefficient printouts to demonstrate that they work.(piease use lisp)
下面是使用 Lisp 定义非负整数流以及级数 S1 和 S2 的代码:
```lisp
;; 定义非负整数流
(defun non-neg-integers ()
(let ((n 0))
(lambda ()
(prog1 n
(setq n (1+ n))))))
;; 定义级数 S1
(defun s1 (x)
(let ((nats (non-neg-integers)))
(labels ((term (n)
(expt x n)))
(loop for n from 0
sum (funcall nats)
collect (term n)))))
;; 定义级数 S2
(defun s2 (x)
(let ((nats (non-neg-integers)))
(labels ((term (n)
(* (funcall nats) (expt x n))))
(loop for n from 0
sum (funcall nats)
collect (term n)))))
;; 打印 S1 和 S2 的前 5 个系数
(format t "S1: ~a~%" (subseq (s1 2) 0 5))
(format t "S2: ~a~%" (subseq (s2 2) 0 5))
```
在上面的代码中,我们使用了 `non-neg-integers` 函数定义了一个返回非负整数流的函数。然后,我们分别使用 `s1` 和 `s2` 函数定义了级数 S1 和 S2。在这两个函数中,我们用 `non-neg-integers` 函数生成了一个非负整数流,并用 `labels` 定义了一个计算每一项系数的函数 `term`,最后使用 `loop` 循环计算级数的前若干项系数,并用 `collect` 函数收集结果。
最后,我们使用 `format` 函数打印了 S1 和 S2 的前 5 个系数。你可以自己尝试修改参数 x,或者调整打印的系数个数,验证它们的正确性。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)