0
我發現很難理解下面的代碼之間的差異:差異從字節指針
auto pixel = static_cast<uint32_t>(D3DCOLOR_RGBA(r, g, b, a));
auto components = (uint8_t*)&pixel;
std::array<uint8_t, 4> pixel_colours = { components[0], components[1], components[2], components[3] };
uint8_t b_a = pixel >> 24;
uint8_t b_r = (pixel >> 16) & 0xFF;
uint8_t b_g = (pixel >> 8) & 0xFF;
uint8_t b_b = pixel & 0xFF;
return static_cast<uint32_t>(D3DCOLOR_RGBA(r, g, b, a));
用於R,G,B,A = {255,128,64,0} ,pixel_colours的值是{ 64, 128, 255, 0}
,而b_a,b_r,b_g,b_b是0, 255, 128, 64
我不明白爲什麼差異正在發生 - 我期望它們是相同的。有人可以解釋嗎?
[Endianness](https://en.wikipedia.org/wiki/Endianness#Atomic_element_size_8-bit_2) –
如果像素是RGBA,不應該是'b_r = pixel >> 24' ...'b_a =像素&0xFF'?看起來第二個例子是ARGB。 –
也可以將你的測試修改爲「{255,128,64,0}」,這樣你可以看到更好的效果。現在你不能區分255和255. –