今天调试颜色值得时候,发现了一个超出我认知常理的事情。
请看如下代码
union TestUnion
{
struct
{
uint8_t b;
uint8_t g;
uint8_t r;
uint8_t a;
};
uint32_t rgba;
};
int main()
{
int intData = (1 << 24) | (2 << 16) | (3 << 8) | 4;
TestUnion* testUnion = (TestUnion*)(&intData);
std::cout << testUnion->b << " " << testUnion->g << " " << testUnion->r << " " << testUnion->a;
system("pause");
return 0;
}
最终输出的结果是是4 3 2 1
经过考虑
强行解释一波
栈从上往下分配内存,所以4在低位,1在高位,对应到联合体里面,b是低位,a是高位,所以b是4,a是1。