Here I leave these useful functions for manipulating bits using Delphi.
//get if a particular bit is 1 function Get_a_Bit(const aValue: Cardinal; const Bit: Byte): Boolean; begin Result := (aValue and (1 shl Bit)) <> 0; end; //set a particular bit as 1 function Set_a_Bit(const aValue: Cardinal; const Bit: Byte): Cardinal; begin Result := aValue or (1 shl Bit); end; //set a particular bit as 0 function Clear_a_Bit(const aValue: Cardinal; const Bit: Byte): Cardinal; begin Result := aValue and not (1 shl Bit); end; //Enable o disable a bit function Enable_a_Bit(const aValue: Cardinal; const Bit: Byte; const Flag: Boolean): Cardinal; begin Result := (aValue or (1 shl Bit)) xor (Integer(not Flag) shl Bit); end;
July 7, 2011 at 6:45 pm
Thank you, its very usefull!
January 3, 2014 at 6:17 am
Since Delphi XE2 (maybe earlier), there is a built-in TBits class for representing bit sets.
January 3, 2014 at 10:53 am
Thanks TLama, I’m aware of the TBits class (since Delphi 7), this entry was written back in 2009 and was provided to work with older versions of Delphi.