首页  编辑  

BIT的操作

Tags: /超级猛料/Alogrith.算法和数据结构/数值运算/   Date Created:

位操作,Bit操作:

shr,and

如读第三位:( x shr 3 ) and 1

/// Check the bit is 0 or 1? 1:True,0:False

function CheckBit(const val: DWORD; const TheBit: byte): boolean;

begin

 result := (val and (1 shl TheBit)) <> 0;

end;

/// Set bit  1

function SetBitOn(const val: DWORD; const TheBit: byte): DWORD;

begin

 result := val or (1 shl TheBit);

end;

/// Set bit 0

function SetBitOff(const val: DWORD; const TheBit: byte): DWORD;

begin

 result := val and ((1 shl TheBit) xor $FFFFFFFF);

end;

/// Toggle the bit,1-->0 ,0-->1

function ToggleBit(const val: DWORD; const TheBit: byte): DWORD;

begin

 result := val xor (1 shl TheBit);

end;