简体   繁体   中英

C++ bit shift which way

I am having trouble understanding which way I should be shifting bits to convert from part of one structure to another. I am writing an application to be used only on Windows / Intel systems.

Old structure (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) 

New structure (ReturnData and 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) 

Bits 0 to 5 should be 0 as that data is unknown in the existing record. I think that converting to the new structure using bit mask and shift:

New->ReturnData = (Old->DataByte & 0x07)>>1 | (Old->DataByte & 0x38)>>2;
New->DataByte = Old->DataByte & 0xC0;

Is that correct? the first 3 bits (& 0x07) shifted >> 1 becomes the first nibble and the second 3 bits (& 0x38) shifted >> 2 the second nibble forming a byte.. or is the shift the other way as Intel is the other endianness?

Bit 0 is bit 0 regardless of endianness. Endianness affects byte order in memory, which should only matter if you want to do reinterpreting or sending data across the wire. Math is always internally consistent.

Bits 0-2 would be 0x07 , Bits 3-5 would be 0b0011 1000 , which is 0x38 . Now in the new data structure, the "return number" stays in the same place, and the "number of returns" just shifts up one (from 3-5) to (4-7). So that's:

New->ReturnData = (Old->DataByte & 0x07) // these bits stay in the same place
      | ((Old->DataByte & 0x38) << 1); // these shift up one

Your logic for Scan+Edge looks correct.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM