Question

I am pulling data from a .txt file byte by byte using delphi. For each byte, I need to swap the beginning and the ending bits of that byte. I've been searching google for hours but I can't find any reference about manipulating bits inside byte vars in delphi. Any ideas?

Was it helpful?

Solution

b := (b and $7E) or (b shr 7) or (b shl 7); 

First we keep the middle 6 bits, then swaps the MSB and the LSB bits in register shift operations.

Bit manipulation in Delphi is fun. There are many options, and this is a straight forward solution. David proposes a LUT solution which can be faster (only by a tiny bit though).

There was an interesting similar question about bit-reflecting a whole byte, see How can I bit-reflect a byte in Delphi?.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top