为什么 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) 我想知道是否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)
为什么我不能得到我期望的结果?