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?
X = Y >> 1
then X is always smaller (or equal if zero) than Y regardless of endianness. - Aitch