Recent

Author Topic: [SOLVED] Bitpacked record of Boolean flags — problem with reading flag states  (Read 2074 times)

furious programming

  • Hero Member
  • *****
  • Posts: 505
  • I click a little.
    • TreeStructInfo — format for text and binary configuration files
Is there such a need if the software is to run exclusively on Windows?

Theoretically, only me will use this program, but I would prefer it not to crash in the future when I change my computer to a newer/different one.
« Last Edit: March 15, 2021, 02:13:19 pm by furious programming »
Lazarus 2.0.12 with FPC 3.2.0 (SVN Revision 64642), Windows 10 — all 64-bit

ASBzone

  • Hero Member
  • *****
  • Posts: 616
  • Automation leads to relaxation...
    • Free Console Utilities for Windows (and a few for Linux) from BrainWaveCC
Is there such a need if the software is to run exclusively on Windows?

Theoretically, only me will use this program, but I would prefer it not to crash in the future when I change my computer to a newer/different one.

Well, since it is only you, you could always recompile it. :)

I don't believe that any Windows editions -- not even the ones for MIPS and DEC Alpha -- were ever Big Endian.
-ASB: https://www.BrainWaveCC.com/

Lazarus v2.0.13 r64843 / FPC v3.2.1-r49055 (via FpcUpDeluxe) -- Windows 64-bit install w/Win32 and Linux/Arm cross-compiles
Primary System: Windows 10 Pro x64, Version 2009 (Build 19042)
Other Systems: Windows 10 Pro x64, Version 2009 (Build 19042) or greater

furious programming

  • Hero Member
  • *****
  • Posts: 505
  • I click a little.
    • TreeStructInfo — format for text and binary configuration files
Well, since it is only you, you could always recompile it. :)

This is true, but while the source code can be recompiled without a problem, there is a problem with the already generated gameplay files, because I would have to write an endianess converter for the new program to understand them.

And that's what I'd rather avoid. I am thinking for the future, because for the time being I will be using this program myself, so one day it may turn into a full-fledged product. And I would not like to have to rewrite the entire recorder code and convert the recording files. It is true that the record files will not be used for sharing (they will only be used for local software needs), but still.
Lazarus 2.0.12 with FPC 3.2.0 (SVN Revision 64642), Windows 10 — all 64-bit

y.ivanov

  • Jr. Member
  • **
  • Posts: 98
IMHO, You should pack them by yourself and not depend on 'packed' feature and the endianess.

Something like:
Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. type
  4.  
  5.   { TMyPacking }
  6.   TMyPacking = class
  7.   private
  8.     function GetBit(AIndex: Integer): Boolean;
  9.     procedure SetBit(AIndex: Integer; AValue: Boolean);
  10.   public
  11.     Data: array [0..512] of Byte;
  12.     property Bit[I: Integer]: Boolean read GetBit write SetBit;
  13.   end;
  14.  
  15.   TTetrisPacking = class(TMyPacking)
  16.   public
  17.     property Left: Boolean index 0 read GetBit write SetBit;
  18.     property Right: Boolean index 1 read GetBit write SetBit;
  19.     // More boolean properties as needed with appropriate index
  20.     // ...
  21.   end;
  22.  
  23. { TMyPacking }
  24.  
  25. function TMyPacking.GetBit(AIndex: Integer): Boolean;
  26. begin
  27.   Result := 0 <> (Data[AIndex div 8] or (1 shl (AIndex mod 8)));
  28. end;
  29.  
  30. procedure TMyPacking.SetBit(AIndex: Integer; AValue: Boolean);
  31. begin
  32.   if AValue then
  33.     Data[AIndex div 8] := Data[AIndex div 8] or (1 shl (AIndex mod 8)) else
  34.     Data[AIndex div 8] := Data[AIndex div 8] and not (1 shl (AIndex mod 8))
  35. end;
  36.  
  37. var
  38.   P: TTetrisPacking;
  39.  
  40. begin
  41.   P := TTetrisPacking.Create;
  42.   P.Left := True;
  43.   P.Right := False;
  44.   // Write or read the P.Data in a byte stream...
  45.   P.Free;
  46. end.
  47.  
  48.  


It can be further extended as you like.

Regards,

winni

  • Hero Member
  • *****
  • Posts: 2330


I don't believe that any Windows editions -- not even the ones for MIPS and DEC Alpha -- were ever Big Endian.

Hi!

DEC Alpha was Big Endian. Nice machine.

Winni

jamie

  • Hero Member
  • *****
  • Posts: 4447
Why not a SET ?
8 enums should fit in a byte.
The only true wisdom is knowing you know nothing

Thaddy

  • Hero Member
  • *****
  • Posts: 10786
You do not need a record in most cases.
sysutils now contains type helpers for most standard ordinal types,
Suppose you need to test 8 flags (byte) we can write this (0..7)
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}
  2. uses sysutils;
  3. var a:byte = 54;i:integer;
  4. begin
  5.   for i := 0 to 7 do
  6.     writeln(i,' ',a.testbit(i);
  7. end.
I implemented this - and more - in syshelph.inc and it is in 3.2.0 and trunk.
So choose am ordinal up to qword, optionally declare a range and no record is needed.
« Last Edit: April 11, 2021, 04:04:40 pm by Thaddy »

Thaddy

  • Hero Member
  • *****
  • Posts: 10786
To demonstrate with a range:
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}
  2. uses sysutils;
  3. type
  4.   Tmyrange = 0..5;
  5.  
  6. var a:byte = 54;i:TmyRange;
  7. begin
  8.   for i := Low(TMyRange) to high(TmyRange) do
  9.     writeln(i,' ',a.testbit(i));
  10. end.
It is also platform agnostic.

Obviously you can also set, clear and toggle the bits through the type helper.
« Last Edit: April 11, 2021, 04:31:00 pm by Thaddy »

Thaddy

  • Hero Member
  • *****
  • Posts: 10786
It can be further extended as you like.
Regards,
See my last answers... not necessary. Comes as standard from 3.2.0.
Just include sysutils in the uses clause. (up to qword range, that's a lot of state bits) Limit a range that is less than an ordinal by definng it. See above example.
Regards,
Thaddy
« Last Edit: April 11, 2021, 04:21:45 pm by Thaddy »

y.ivanov

  • Jr. Member
  • **
  • Posts: 98
It can be further extended as you like.
Regards,
See my last answers... not necessary. Comes as standard.

Yup, very nice!

As far as I understand, the problem is how to pack/save/restore a large bit array but still use a meaningful symbolic names. So my suggestion for using getters/setters to pack them in a byte array and not to worry about endianess.

No doubt, It can be achieved in different ways.

Regards,

PascalDragon

  • Hero Member
  • *****
  • Posts: 2979
  • Compiler Developer


I don't believe that any Windows editions -- not even the ones for MIPS and DEC Alpha -- were ever Big Endian.

Hi!

DEC Alpha was Big Endian. Nice machine.

No, DEC Alpha could run as both big and little endian and Windows always used little endian on processors that support both (MIPS, DEC Alpha, PowerPC, ARM, Aarch64).

ASBzone

  • Hero Member
  • *****
  • Posts: 616
  • Automation leads to relaxation...
    • Free Console Utilities for Windows (and a few for Linux) from BrainWaveCC

I don't believe that any Windows editions -- not even the ones for MIPS and DEC Alpha -- were ever Big Endian.

Hi!

DEC Alpha was Big Endian. Nice machine.

Winni

It is my understanding that DEC Alpha is bi-endian, and that it ran in little endian mode for Windows.

https://en.wikipedia.org/wiki/DEC_Alpha
-ASB: https://www.BrainWaveCC.com/

Lazarus v2.0.13 r64843 / FPC v3.2.1-r49055 (via FpcUpDeluxe) -- Windows 64-bit install w/Win32 and Linux/Arm cross-compiles
Primary System: Windows 10 Pro x64, Version 2009 (Build 19042)
Other Systems: Windows 10 Pro x64, Version 2009 (Build 19042) or greater

ASBzone

  • Hero Member
  • *****
  • Posts: 616
  • Automation leads to relaxation...
    • Free Console Utilities for Windows (and a few for Linux) from BrainWaveCC
No, DEC Alpha could run as both big and little endian and Windows always used little endian on processors that support both (MIPS, DEC Alpha, PowerPC, ARM, Aarch64).


LOL I ended up replying to a message I already had open, and totally missed your answer.  Would have saved me some time.
-ASB: https://www.BrainWaveCC.com/

Lazarus v2.0.13 r64843 / FPC v3.2.1-r49055 (via FpcUpDeluxe) -- Windows 64-bit install w/Win32 and Linux/Arm cross-compiles
Primary System: Windows 10 Pro x64, Version 2009 (Build 19042)
Other Systems: Windows 10 Pro x64, Version 2009 (Build 19042) or greater

 

TinyPortal © 2005-2018