Bit-packed structure (record) in Free Pascal and Delphi
Here is the code explaining some methods to pack bit accessing logic in your program.
program project1;
type
TRec1 = packed record // Doesn't pack at all
b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, b13, b14, b15, b16: boolean;
end;
TRec2 = bitpacked record // Free Pascal specific
b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, b13, b14, b15, b16: boolean;
end;
TRecByte = (b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, b13, b14, b15, b16);
TRecBytes = set of TRecByte;
var
r1: TRec1;
r2: TRec2;
s1: TRecBytes;
begin
writeln(SizeOf(r1), ' byte(s)'); // returns 16 bytes
writeln(SizeOf(r2), ' byte(s)'); // returns 2 bytes
writeln(SizeOf(s1), ' byte(s)'); // returns 4 bytes
end.
See also: FP bits manipulations
blog comments powered by Disqus