Home page > Delphi, tips bulk > Tips for beginners > FlagFunctions: manipulate bits in Cardinals
FlagFunctions: manipulate bits in Cardinals
Friday 26 August 2011, by
This Delphi unit shows how to work on the bits of an integer variable, which can store several flags in one variable.
The Windows API uses a lot this way to pass parameters.
Note however that the Pascal language can do the same thing more elegantly with the types "set".
- (*********************************************************)
- (***) (***)
- (***) UNIT FlagFunctions; (***)
- (***) (***)
- (*********************************************************)
-
- (*********************************************************)
- (* Feel free to use it, but at your own risk! *)
- (* À utiliser librement, mais à vos risques et périls ! *)
- (* CapJack - http://capjack.fr *)
- (*********************************************************)
-
-
- (*********************************************************)
- (***) (***)
- (***) INTERFACE (***)
- (***) (***)
- (*********************************************************)
-
-
- (*********************************************************)
- (* DTSetMask, DTOneBitSet, DTAllBitsSet, DTBitIsSet *)
- (* ------------------------------------------------------*)
- (* Set of tools to manipulate bits in Cardinals. *)
- (* ------------------------------------------------------*)
- (* Jeu d'outils pour manipuler les bits des Cardinaux. ;)*)
- (*********************************************************)
-
- const DTMask : array [0..31] of Cardinal = (
- $00000001,$00000002,$00000004,$00000008,
- $00000010,$00000020,$00000040,$00000080,
- $00000100,$00000200,$00000400,$00000800,
- $00001000,$00002000,$00004000,$00008000,
- $00010000,$00020000,$00040000,$00080000,
- $00100000,$00200000,$00400000,$00800000,
- $01000000,$02000000,$04000000,$08000000,
- $10000000,$20000000,$40000000,$80000000);
-
- {
- Sets to 1 (if AFlag = true), or to 0 (if AFlag = false)
- the bit(s) of AValue designated by AMask.
- Positionne à 1 (si AFlag = true) ou à 0 (si AFlag = false)
- le(s) bit(s) de AValue désignés par AMask.
- }
- Procedure DTSetMask ( var AValue, AMask : Cardinal;
- const AFlag : Boolean );
-
-
- {
- Returns true if ONE of the bits of AValue designated
- by AMask is set to 1.
- Renvoie true si l'UN des bits de Value désignés
- par AMask est positionné à 1.
- }
- Function DTOneBitSet( const AValue, AMask: Cardinal ): Boolean;
-
-
- {
- Returns true if ALL the bits of AValue designated
- by AMask are set to 1.
- Renvoie true si TOUS les bits de Value désignés
- par AMask sont positionnés à 1.
- }
- Function DTAllBitsSet(const AValue,AMask: Cardinal):boolean;
-
-
- {
- Returns true if the bit n° ANumbit of AValue is set to 1.
- Renvoie true si le bit n° ANumBit de AValue vaut 1.
- }
- Function DTBitIsSet(const ANumBit, AValue : Cardinal): Boolean;
-
-
- (*********************************************************)
- (***) (***)
- (***) IMPLEMENTATION (***)
- (***) (***)
- (*********************************************************)
-
- {---------------------------------------------------------}
-
- Procedure DTSetMask ( var AValue, AMask : Cardinal;
- const AFlag : Boolean );
- begin
- if AFlag
- then AValue := AValue or AMask
- else AValue := AValue and not(AMask);
- end;
-
- {---------------------------------------------------------}
-
- Function DTOneBitSet( const AValue, AMask: Cardinal ): Boolean;
- begin
- Result := ( ( AValue and AMask ) <> 0 )
- end;
-
- {---------------------------------------------------------}
-
- Function DTAllBitsSet( const AValue, AMask: Cardinal ): Boolean;
- begin
- Result := ( ( AValue and AMask) = AMask )
- end;
-
- {---------------------------------------------------------}
-
- Function DTBitIsSet( const ANumBit, AValue: Cardinal ): Boolean;
- begin
- Result := ( AValue and DTMask[ANumBit] ) <> 0;
- end;
-
- {---------------------------------------------------------}
-
- END.