gpt4 book ai didi

c++ - 带负值的 Unsigned long

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:48:34 25 4
gpt4 key购买 nike

请看下面的简单代码:

#include <iostream>
#include <stdlib.h>

using namespace std;


int main(void)
{
unsigned long currentTrafficTypeValueDec;
long input;
input=63;
currentTrafficTypeValueDec = (unsigned long) 1LL << input;
cout << currentTrafficTypeValueDec << endl;
printf("%u \n", currentTrafficTypeValueDec);
printf("%ld \n", currentTrafficTypeValueDec);
return 0;
}

为什么 printf() 显示负值的 currentTrafficTypeValueDec (unsigned long)?

输出是:

9223372036854775808
0
-9223372036854775808

最佳答案

%d 是一个签名的格式化程序。将 currentTrafficTypeValueDec 的位(2 的 63 次方)重新解释为 signed long 给出负值。所以 printf() 打印一个负数。

也许你想使用 %lu

关于c++ - 带负值的 Unsigned long,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2455806/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com