#include <iostream>
#include <stdint.h>
struct Foo
{
int a : 2;
int b : 2;
int c : 2;
int d : 2;
};
int main()
{
Foo foo;
foo.d = 2;
std::cout << sizeof(foo) << std::endl;
std::cout << foo.d << std::endl;
return 0;
}
用g++,结果变成了4 -2
,foo.d怎么变成-2了?
最佳答案
int
默认是有符号的,所以 int :2
的范围是 -2 到 1。使用 unsigned int :2
如果您希望能够存储值 0 到 3。
关于c++ - 为什么 c 位域中的 unsigned int 值变成了 signed 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21620848/