[英]Bitwise shift for unsigned long long type
In ac program. 在交流程序中。 I am trying to use the left shift operator on uint64_t variable.
我试图在uint64_t变量上使用左移运算符。
Eg 例如
// with shift of 24 bits
uint64_t x = 0;
x = (((uint64_t)76) << (24));
Output is: x = 1275068416
---------------------------------------------
// with shift of 32 bits
uint64_t x = 0;
x = (((uint64_t)76) << (32));
Output is: x = 0
If I perform left shift till 24 bits then it works fine, but at 32 bits it outputs 0. Whereas what I think is as the size of uint64_t ie unsigned long long is 64 bits. 如果我执行左移直到24位,那么它可以正常工作,但是在32位时,它输出0。而我认为是uint64_t的大小,即unsigned long long是64位。 So shouldn't it work till the 64 bit shift ?
那么,直到64位移位才有效吗?
You're using the wrong format specifier to print the output. 您使用了错误的格式说明符来打印输出。 The
%d
format specifier expects an int
, which apparently is 32-bit on your system. %d
格式说明符期望一个int
,它在您的系统上显然是32位的。 So passing a 64-bit value (and an unsigned one at that) leads to undefined behavior. 因此,传递64位值(以及该值处的无符号值)会导致未定义的行为。
You should use the PRIu64
macro to get the correct format specifier for an unsigned 64-bit value. 您应该使用
PRIu64
宏为无符号的64位值获取正确的格式说明符。
printf("%"PRIu64"\n", x);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.