[英]Why is (int64_t)-1 + (uint32_t)0 signed?
Why is (int64_t)-1 + (uint32_t)0
signed in C? 为什么
(int64_t)-1 + (uint32_t)0
在C中签名? It looks like it's int64_t
, but my intuition would say uint64_t
. 看起来它是
int64_t
,但我的直觉会说uint64_t
。
FYI When I run 仅供参考我跑的时候
#include <stdint.h>
#include <stdio.h>
#define BIT_SIZE(x) (sizeof(x) * 8)
#define IS_UNSIGNED(x) ((unsigned)(((x) * 0 - 1) >> (BIT_SIZE(x) - 1)) < 2)
#define DUMP(x) dump(#x, IS_UNSIGNED(x), BIT_SIZE(x))
static void dump(const char *x_str, int is_unsigned, int bit_size) {
printf("%s is %sint%d_t\n", x_str, "u" + !is_unsigned, bit_size);
}
int main(int argc, char **argv) {
(void)argc; (void)argv;
DUMP(42);
DUMP(42U);
DUMP(42L);
DUMP(42UL);
DUMP(42LL);
DUMP(42ULL);
DUMP('x');
DUMP((char)'x');
DUMP(1 + 2U);
DUMP(1 << 2U);
DUMP((int32_t)-1 + (uint64_t)0);
DUMP((int64_t)-1 + (uint32_t)0);
return 0;
}
I get the following output: 我得到以下输出:
42 is int32_t
42U is uint32_t
42L is int32_t
42UL is uint32_t
42LL is int64_t
42ULL is uint64_t
'x' is int32_t
(char)'x' is int8_t
1 + 2U is uint32_t
1 << 2U is int32_t
(int32_t)-1 + (uint64_t)0 is uint64_t
(int64_t)-1 + (uint32_t)0 is int64_t
Why is (int64_t)-1 + (uint32_t)0 signed?
为什么(int64_t)-1 +(uint32_t)0签名?
Because int64_t
conversion rank is greater than uin32_t
conversion rank. 因为
int64_t
转换等级大于uin32_t
转换等级。 (uint32_t)0
is converted to int64_t
in the +
expression and int64_t
is the type of the resulting expression. (uint32_t)0
在+
表达式中转换为int64_t
, int64_t
是结果表达式的类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.