Scalabium Software

SMExport advertising
Knowledge for your independence'.
Home Delphi and C++Builder tips

#99: How can I works with bits?

In own development I like the works with bits.

For example, I like to combine a some flags of status on one byte for storing. Fine sample is attributes of file in operating system:
1. system
2. hidden
3. readonly
4. archive
5. volume
6. directory

The other fine sample is font style:
1. bold
2. italic
3. underline

Of course, you can store these values of set in separated boolean variables but I prefers to store them in one byte. For example, the 110 (in binary) for font style is bold and italic, the 100 is bold...

In such storage you must have the methods to access on some bit:
1. compare on check
2. set the bit
3. clear the bit
4. toggle the bit

The next procedures allows to realize these tasks

1. compare a state of bit

function GetBitState(Value, NumBits: Word): Boolean;
  Result := (Value and NumBit = NumBit)

2. set a bit.

procedure SetBit(var Value, NumBits: Word);
  Value := Value or NumBit

3. clear a bit

procedure ClearBit(var Value, NumBits: Word);
  Value := Value or NumBit
  Value := Value xor NumBit

4. toggle a state of bit

procedure ToggleBit(var Value, NumBits: Word);
  Value := Value xor NumBit

Published: October 2, 2000

See also
Paradox Viewer
DBISAM Password Recovery
Paradox ActiveX
Viewer for TNEF-files (winmail.dat)
Fast Document Viewer
Metafile Convert
ABA Spreadsheet Convert
Clarion to Text converter

Contact to webmaster


Borland Software Code Gear Scalabium Delphi tips

Copyright© 1998-2022, Scalabium Software. All rights reserved.

SMExport advertising