|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
The other fine sample is font style:
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
The next procedures allows to realize these tasks
1. compare a state of bit
function GetBitState(Value, NumBits: Word): Boolean; begin Result := (Value and NumBit = NumBit) end;
2. set a bit.
procedure SetBit(var Value, NumBits: Word); begin Value := Value or NumBit end;
3. clear a bit
procedure ClearBit(var Value, NumBits: Word); begin Value := Value or NumBit Value := Value xor NumBit end;
4. toggle a state of bit
procedure ToggleBit(var Value, NumBits: Word); begin Value := Value xor NumBit end;
Copyright© 1998-2022, Scalabium
Software. All rights reserved.