【发布时间】:2020-04-06 09:02:58
【问题描述】:
#include <iostream>
#define TRY_INT
void testRun()
{
#ifdef TRY_INT //test with unsigned
unsigned int value1{1}; //define some unsigned variables
unsigned int value2{1};
unsigned int value3{2};
#else //test with fixed width
uint16_t value1{1}; //define fixed width unsigned variables
uint16_t value2{1};
uint16_t value3{2};
#endif
if ( value1 > value2 - value3 )
{
std::cout << value1 << " is bigger than: " << value2 - value3 << "\n";
}
else
{
std::cout << value1 << " is smaller than: " << value2 - value3 << "\n";
}
}
int main()
{
testRun();
return 0;
}
我得到无符号整数:
1 is smaller than: 4294967295
固定宽度的无符号整数,输出为:
1 is smaller than: -1
我的期望是它也会环绕,这与 std::cout 有关系吗?
【问题讨论】:
-
-1 的位表示(2 补码)是什么?
-
@RichardCritten, 0xFFFFFFFF?
标签: c++ unsigned-integer