DelphiBasics
TBits
Type
An object that can hold an infinite number of Boolean values Classes unit
type TBits;
Description
The TBits type holds a flexible collection of bits (Boolean values).
 
The colection size can be altered at any time (using the size property).
 
Bits are accessed using the Bits property, like this :
 
flag := myBits.Bits[2];
 
or, more simply:
 
flag := myBits[2];
 
There is one utility function - OpenBit - which returns the index of the first false value. There is no equivalent for true values.
Related commands
ArrayA data type holding indexable collections of data
BooleanAllows just True and False values
 Download this web site as a Windows program.




 
Example code : A simple example
var
  flags : TBits;        // Our variable collection of Boolean values
  i : Integer;
begin
  // Create our TBits object
  flags := TBits.Create;

  // Add a few items to our Boolean flags collection
  flags.Size := 5;

  // And set a few values
  flags[0] := true;
  flags[1] := true;
  flags[4] := true;

  // Now display the contents of the collection
  // Note that indexing starts at 0
  for i := 0 to flags.Size-1 do
    if flags[i] = true
    then ShowMessageFmt('Bit %d is true',[i])
    else ShowMessageFmt('Bit %d is false',[i]);

  // TBits has one main method -
  // to find the index of the first false value
  ShowMessageFmt('Index of the first false value is %d',[flags.OpenBit]);
end;
Show full unit code
  Bit 0 is true
  Bit 1 is true
  Bit 2 is false
  Bit 3 is false
  Bit 4 is true
  Index of the first false value is 2
  
 
Delphi Programming © Neil Moffatt . All rights reserved.  |  Home Page