【发布时间】:2015-02-09 00:13:59
【问题描述】:
我无法理解应该以哪种方式将位从一个结构的一部分转换为另一个结构。我正在编写一个仅用于 Windows / Intel 系统的应用程序。
旧结构(DataByte):
Return Number 3 bits (bits 0 – 2)
Number of Returns 3 bits (bits 3 – 5)
Scan Direction Flag 1 bit (bit 6)
Edge of Flight Line 1 bit (bit 7)
新结构(ReturnData 和 DataByte):
Return Number 4 bits (bits 0 - 3)
Number of Returns (given pulse) 4 bits (bits 4 - 7)
Classification Flags 4 bits (bits 0 - 3)
Scanner Channel 2 bits (bits 4 - 5)
Scan Direction Flag 1 bit (bit 6)
Edge of Flight Line 1 bit (bit 7)
位 0 到 5 应为 0,因为该数据在现有记录中是未知的。我认为使用位掩码和移位转换为新结构:
New->ReturnData = (Old->DataByte & 0x07)>>1 | (Old->DataByte & 0x38)>>2;
New->DataByte = Old->DataByte & 0xC0;
正确吗?前 3 位 (& 0x07) 移位 >> 1 成为第一个 nibble 和第二个 3 位 (& 0x38) 移位 >> 2 第二个 nibble 形成一个字节.. 或者是英特尔的另一种方式是另一个字节序吗?
【问题讨论】:
-
当移位时你不必关心字节顺序。当您拥有
X = Y >> 1时,无论字节序如何,X 总是小于(或等于如果为零)Y。
标签: c++ bit-manipulation