小编Bra*_*Qin的帖子

为什么 x+0 和 x|0 有不同的结果?

为什么 x+0 和 x|0 有不同的结果?

下面是我的代码。

我的环境是 WSL (Debian Sid) + GCC 10.2.1。

#include <stdio.h>

/**
 * Do rotating left shift. Assume 0 <= n < w
 * Examples when x = 0x12345678 and w = 32:
 * n = 4 -> 0x23456781, n = 20 -> 0x67812345
 */
unsigned rotate_left(const unsigned x, const int n)
{
    const int w = sizeof(unsigned) << 3;
    return (x << n) + (x >> (w - n));
}

int main()
{
    int x …
Run Code Online (Sandbox Code Playgroud)

c c++

1
推荐指数
1
解决办法
157
查看次数

当 x=0x80000000,y = 1(32 位补码)时,为什么 `x - y &lt;= x` 为真?

我想知道是否x - y溢出。

下面是我的代码。

#include <stdio.h>

/* Determine whether arguments can be subtracted without overflow */
int tsub_ok(int x, int y)
{
    return (y <= 0 && x - y >= x) || (y >= 0 && x - y <= x);
}

int main()
{
    printf("0x80000000 - 1 : %d\n", tsub_ok(0x80000000, 1));
}
Run Code Online (Sandbox Code Playgroud)

为什么我不能得到我期望的结果?

c

0
推荐指数
1
解决办法
104
查看次数

标签 统计

c ×2

c++ ×1