1

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?

1
  • when shifting bits you don't have to care about endianness. When you have X = Y >> 1 then X is always smaller (or equal if zero) than Y regardless of endianness. Commented Feb 9, 2015 at 0:22

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for clearing that up Barry, so I did have it the wrong way around and endianness is more to do with bytes in words than bytes themselves.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.