Difference between revisions of "BitHelpers"

From Lazarus wiki
Jump to navigationJump to search
(From forum)
 
Line 17: Line 17:
 
Example code and it's output:
 
Example code and it's output:
  
<sourcecode>
+
<syntaxhighlight>
 
   uses
 
   uses
 
     bithelpers;
 
     bithelpers;
Line 33: Line 33:
 
     Memo1.Append(MyBool.ToString('Укључено', 'Искључено', scfUpperCase)); // when case and unicode matter
 
     Memo1.Append(MyBool.ToString('Укључено', 'Искључено', scfUpperCase)); // when case and unicode matter
 
   end;  
 
   end;  
</sourcecode>
+
</syntaxhighlight>
  
 
==TByteBitHelper==
 
==TByteBitHelper==
Line 39: Line 39:
 
Example code and it's output:
 
Example code and it's output:
  
<sourcecode>
+
<syntaxhighlight>
 
   procedure TForm1.ByteBitTestBtnClick(Sender: TObject);
 
   procedure TForm1.ByteBitTestBtnClick(Sender: TObject);
 
   var
 
   var
Line 52: Line 52:
 
     Memo1.Append(MyByte.ToBooleanString);                      // show leading zeros   
 
     Memo1.Append(MyByte.ToBooleanString);                      // show leading zeros   
 
   end;
 
   end;
</sourcecode>
+
</syntaxhighlight>
  
 
==TWordBitHelper==
 
==TWordBitHelper==
Line 58: Line 58:
 
Example code and it's output:
 
Example code and it's output:
  
<sourcecode>
+
<syntaxhighlight>
 
   procedure TForm1.WordBitTestBtnClick(Sender: TObject);
 
   procedure TForm1.WordBitTestBtnClick(Sender: TObject);
 
   var
 
   var
Line 73: Line 73:
 
     Memo1.Append(MyWord.ToBooleanString);                    // show leading zeros
 
     Memo1.Append(MyWord.ToBooleanString);                    // show leading zeros
 
   end;  
 
   end;  
</sourcecode>
+
</syntaxhighlight>

Revision as of 20:22, 27 September 2018

About

BitHelpers enable additional bit manipulation for qword, longword, word, byte and boolean types which will make your life much easier if you need such a feature.

History

FreePascal type helpers TBooleanHelper, TByteHelper, TWordHelper, TCardinalHelper and TQWordHelper do not offer much when bit manipulation and presentation are needed. That's where BitHelpers package jumps in, nicely extending mentioned type helpers.

Installation

While you can simply copy bithelpers unit to your project directory and start using it, the recommended way would be to open "bithelpers_pkg.lpk" package and compile it. That would add BitHelpers source directory to Lazarus and make it available to all your projects.

Usage

TBooleanBitHelper

Example code and it's output:

  uses
    bithelpers;
  ...
  procedure TForm1.BooleanBitTestBtnClick(Sender: TObject);
  var
    MyBool: boolean;
  begin
    MyBool := true;
    Memo1.Append(MyBool.ToOneZeroString);
    Memo1.Append(MyBool.ToOnOffString); // default is scfUnchangedCase and can be ommited
    Memo1.Append(MyBool.ToOnOffString(scfLowerCase));
    Memo1.Append(MyBool.ToTrueFalseString(scfUpperCase));
    Memo1.Append(MyBool.ToString('OnState', 'OffState')); // true/false custom strings
    Memo1.Append(MyBool.ToString('Укључено', 'Искључено', scfUpperCase)); // when case and unicode matter
  end;

TByteBitHelper

Example code and it's output:

  procedure TForm1.ByteBitTestBtnClick(Sender: TObject);
  var
    MyByte: byte;
  begin
    MyByte.Clear;                                  // %00000000 MyByte equals 0
    MyByte.Bit[0] := true;                         // %00000001 MyByte equals 1
    MyByte.Bit[2] := true;                         // %00000101 MyByte equals 5
    Memo1.Append(MyByte.ToString);
    Memo1.Append('$' + MyByte.ToHexString);
    Memo1.Append(MyByte.ToBooleanString(lzHideLeadingZeros));   // hide leading zeros
    Memo1.Append(MyByte.ToBooleanString);                       // show leading zeros   
  end;

TWordBitHelper

Example code and it's output:

  procedure TForm1.WordBitTestBtnClick(Sender: TObject);
  var
    MyWord: word;
  begin
    MyWord.Clear;                  // %0000000000000000 MyWord equals 0
    MyWord.Byte[0] := 2;           // %0000000000000010 MyWord equals 2
    MyWord.Byte[1] := 1;           // %0000000100000010 MyWord equals 258 (2 + 256)
    MyWord.Byte[1].Bit[7] := true; // %0000000100000010 MyWord equals 258 (Beware!!! This DOES NOT set a bit in MyWord !!!)
    MyWord.Bit[10] := true;        // %0000010100000010 MyWord equals 1282 (258 + 2^10)
    Memo1.Append(MyWord.ToString);
    Memo1.Append('$' + MyWord.ToHexString);
    Memo1.Append(MyWord.ToBooleanString(lzHideLeadingZeros)); // hide leading zeros
    Memo1.Append(MyWord.ToBooleanString);                     // show leading zeros
  end;