Recent

Author Topic: Is there a package to set, clear and read the bits in an integer in Lazarus?  (Read 4468 times)

vfclists

  • Hero Member
  • *****
  • Posts: 1013
    • HowTos Considered Harmful?
Is there a package to set, clear and read the bits in an integer in Lazarus?

I have already viewed http://wiki.lazarus.freepascal.org/Bit_manipulation and want to know if there is a ready made one already, otherwise I will adapt it.
Lazarus 3.0/FPC 3.2.2

jdlinke

  • Jr. Member
  • **
  • Posts: 62
  • Just old me
Not that I'm aware of, but if you work on this and are willing to share your work, could you please update this thread with your results? I'd be very interested.

Thanks.
Lazarus 1.2.4 32-bit version on Windows 8.1 64-bit, Windows 7 64-bit, and Windows XP 32-bit

Currently developing TimberLog and the VirtualDBScroll Component Package

taazz

  • Hero Member
  • *****
  • Posts: 5368
there is tbits in the RTL (classes.pas) and the TBitArray but it doesn't support integer type result.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

yamer

  • Jr. Member
  • **
  • Posts: 87
May be you can use the TBitSet class of GContnrs.

http://yann.merignac.free.fr/unit-gcontnrs.html


vfclists

  • Hero Member
  • *****
  • Posts: 1013
    • HowTos Considered Harmful?
I needed something to help me use the Tag property of a component as bit set, so the code examples in http://wiki.lazarus.freepascal.org/Bit_manipulation are enough for the time being

Just a warning: The SetBit procedure is wrong, 'State' is not included in the parameters.

Quote
procedure SetBit(var Value: QWord; Index: Byte);
begin
  Value := Value or (QWord(State) shl Index);
end

The last two examples are better and are all I need. Setting 'State' to True or False in PutBit clears or sets the bit.
Lazarus 3.0/FPC 3.2.2

ttomas

  • Full Member
  • ***
  • Posts: 245
You can use bitpacked records
Code: [Select]
TInt32Bits = bitpacked record
  Bit0, Bit1, Bit2, Bit3, Bit4, Bit5, Bit6, Bit7,...,Bit31: 0..1;
end;
PInt32Bits = TInt32Bits^;
...
var i: Integer;
     pi: PInt32Bits;
begin
   i := 0;
   pi := @i;
   pi.Bit1 := 1;
   pi.Bit25 := 1;

end;


BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
I needed something to help me use the Tag property of a component as bit set, so the code examples in http://wiki.lazarus.freepascal.org/Bit_manipulation are enough for the time being

Just a warning: The SetBit procedure is wrong, 'State' is not included in the parameters.
Why not correct the wiki page? It's what it's there for.
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

vfclists

  • Hero Member
  • *****
  • Posts: 1013
    • HowTos Considered Harmful?
I needed something to help me use the Tag property of a component as bit set, so the code examples in http://wiki.lazarus.freepascal.org/Bit_manipulation are enough for the time being

Just a warning: The SetBit procedure is wrong, 'State' is not included in the parameters.
Why not correct the wiki page? It's what it's there for.

I am not that familiar with the code design and the original authors intentions.

I need to take some time see how the shr, shl, xor etc functions work myself.

Right now I am just accepting it as it is, and the GetBit and PutBit code appear sane enough.
Lazarus 3.0/FPC 3.2.2

 

TinyPortal © 2005-2018