Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Manipulating bits of byte vars in Delphi

Tags:

byte

delphi

bits

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?

like image 700
HHH Avatar asked Jun 11 '13 14:06

HHH


1 Answers

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?.

like image 102
LU RD Avatar answered Sep 18 '22 14:09

LU RD