从字节指针读取位的差异
Differences in reading bits from byte pointers
我发现很难理解以下代码之间的差异:
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_b0, 255, 128, 64
我不明白为什么会发生差异 - 我希望它们是相同的。有人可以解释一下吗?
components[0]
...components[1]
以字节数组的形式访问内存,并按照内存中列出的顺序读取。pixel >> 24
......pixel & 0xFF
访问 int 的逻辑值。由于x86和x64(英特尔(架构使用小端序,因此两者有所不同。关于Endianess的Wiki文章解释了所有细节。
相关文章:
- 在java中读取c++字节的位字段
- 从 C++ 中的标准输入迭代读取字节
- 写入/dev/ttyS0后读取0字节
- 提升 ASIO transfer_exactly读取 0 字节
- 读取字节后丢失指针
- 如何从文件中读取字节?C++
- AWS S3 CPP SDK读取字节多于指定的范围
- 从二进制文件中保存的 4 字节双精度读取 8 字节双精度
- 从 midi 文件读取字节时出现问题
- 串行读取字节 - Arduino 循环
- 将整数从处理发送到Arduino,无法读取字节
- 从文件到短 /长整数读取字节
- 使用C 中的字符指针从字符数组中读取字节
- 正在尝试读取4字节无符号整数的二进制文件并转换为伏特
- 从二进制文件中逐个读取字节
- 正在从二进制文件中读取字节
- 如何从无符号变量写入和读取字节
- 使用 ifstream 读取字节
- 从串行设备读取字节(并理解它们??)
- 如何从一个套接字读取' 1 '字节,而从另一个套接字读取' read_some '字节