Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2019-05

Bitpacked bug?

5 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Victor Campillo

Hi,

I think I found a bug with bitpackeds records.

I have the next record.

   TBitpacked = packed record
     Case Integer Of
       0 : (Word :  Word);
       1 : (Byte :  array [0..1] of Byte);
       2 : (Boolean: bitpacked Array [0..15] of boolean);
       3 : (Bit : bitpacked Record
           case integer of
             0: (bit0: Boolean);
             1: (bit1: Boolean);
             2: (bit2: Boolean);
             3: (bit3: Boolean);
             4: (bit4: Boolean);
             5: (bit5: Boolean);
             6: (bit6: Boolean);
             7: (bit7: Boolean);
             8: (bit8: Boolean);
             9: (bit9: Boolean);
             10: (bit10: Boolean);
             11: (bit11: Boolean);
             12: (bit12: Boolean);
             13: (bit13: Boolean);
             14: (bit14: Boolean);
             15: (bit15: Boolean);
           end);
     end;

See the program below.

var
   pp:TBitpacked;
begin
   pp.Word:=0;
   pp.Boolean[1]:=true;
   writeln(pp.Byte[0]); // This prints 2 -- OK
   writeln(pp.Boolean[0]); // This prints False -- OK
   writeln(pp.Boolean[1]); // This prints True -- OK
   pp.Word:=0;
   pp.Bit.bit1:=true;
   writeln(pp.Byte[0]); // This prints 1 -- WRONG
   writeln(pp.Boolean[0]); // This prints True -- WRONG
   writeln(pp.Boolean[1]); // This prints False -- WRONG
   pp.Word:=0;
   pp.Boolean[8]:=true;
   writeln(pp.Byte[0]); // This prints 0 -- OK
   writeln(pp.Byte[1]); // This prints 1 -- OK
   writeln(pp.Boolean[0]); // This prints False -- OK
   writeln(pp.Boolean[8]); // This prints True -- OK
   pp.Word:=0;
   pp.Bit.bit8:=true;
   writeln(pp.Byte[0]); // This prints 1 -- WRONG
   writeln(pp.Byte[1]); // This prints 0 -- WRONG
   writeln(pp.Boolean[0]); // This prints True -- WRONG
   writeln(pp.Boolean[8]); // This prints False -- WRONG
end.

Is this a known issue or there is something wrong with my code?

I tested this with FPC 3.0.4 and trunk, same result.

Best Regards.

Victor Campillo
Luca Olivetti
to Victor Campillo

El 12/5/19 a les 17:58, Victor Campillo ha escrit:

Show quoted text 31 lines
Hi, I think I found a bug with bitpackeds records. I have the next record.   TBitpacked = packed record     Case Integer Of       0 : (Word :  Word);       1 : (Byte :  array [0..1] of Byte);       2 : (Boolean: bitpacked Array [0..15] of boolean);       3 : (Bit : bitpacked Record           case integer of             0: (bit0: Boolean);             1: (bit1: Boolean);             2: (bit2: Boolean);             3: (bit3: Boolean);             4: (bit4: Boolean);             5: (bit5: Boolean);             6: (bit6: Boolean);             7: (bit7: Boolean);             8: (bit8: Boolean);             9: (bit9: Boolean);             10: (bit10: Boolean);             11: (bit11: Boolean);             12: (bit12: Boolean);             13: (bit13: Boolean);             14: (bit14: Boolean);             15: (bit15: Boolean);           end);     end;

With that last case, aren't you just defining one bit in the bitpacked record?
In that case

Show quoted text 5 lines
  pp.Word:=0;   pp.Bit.bit1:=true;   writeln(pp.Byte[0]); // This prints 1 -- WRONG   writeln(pp.Boolean[0]); // This prints True -- WRONG   writeln(pp.Boolean[1]); // This prints False -- WRONG

this would be correct.

Bye

Luca
Jeppe Johansen
to Victor Campillo: I think I found a bug with bitpackeds records. I have the next record.   TBitpacked =…
Victor Campillo wrote 18 lines
case integer of             0: (bit0: Boolean);             1: (bit1: Boolean);             2: (bit2: Boolean);             3: (bit3: Boolean);             4: (bit4: Boolean);             5: (bit5: Boolean);             6: (bit6: Boolean);             7: (bit7: Boolean);             8: (bit8: Boolean);             9: (bit9: Boolean);             10: (bit10: Boolean);             11: (bit11: Boolean);             12: (bit12: Boolean);             13: (bit13: Boolean);             14: (bit14: Boolean);             15: (bit15: Boolean);           end);

With this layout bit0 to bit15 will all point to bit0. You probably didn't mean to do the case inside the bitpacked record

Victor Campillo
to Jeppe Johansen
Jeppe Johansen wrote
With this layout bit0 to bit15 will all point to bit0. You probably didn't mean to do the case inside the bitpacked record

Yes, you are right, I don't want the case there, copy/paste error.

One hour looking that record declaration and I could not point out that mistake, time to left the laptop and grab a beer :)

Thank you Jeppe.

Best Regards.

Victor Campillo