All pastes #11138 Raw Edit

Someone

public text v1 · immutable
#11138 ·published 2005-05-06 20:42 UTC
rendered paste body
function ExtractBits(const aValue, aStart, aCount: Integer): Integer;
const
  Mask : array[0..31] of Integer = (
    $01,        $03,        $07,        $0F,
    $1F,        $3F,        $7F,        $FF,
    $01FF,      $03FF,      $07FF,      $0FFF,
    $1FFF,      $3FFF,      $7FFF,      $FFFF,
    $01FFFF,    $03FFFF,    $07FFFF,    $0FFFFF,
    $1FFFFF,    $3FFFFF,    $7FFFFF,    $FFFFFF,
    $01FFFFFF,  $03FFFFFF,  $07FFFFFF,  $0FFFFFFF,
    $1FFFFFFF,  $3FFFFFFF,  $7FFFFFFF,  $FFFFFFFF
    );
asm
  xchg ecx,edx
  test edx,edx
  jnz @@isoke
  xor eax,eax
  jmp @@ending
 @@isoke:
  dec edx
  and edx,31
  shr eax,cl
  and eax,dword ptr [Mask+edx*4]
 @@ending:
end;

function GetBit(a, n : Integer): Integer;
begin
  result := 1 and (a shr n);
end;

function GetBitState(a, n : Integer): Boolean;
begin
  result := (1 and (a shr n)) > 0;
end;

procedure SetBitOff(var a : Integer; n : Integer);
begin
  a := a and (not(1 shl n));
end;

procedure SetBitOn(var a : Integer; n : Integer);
begin
  a := a or (1 shl n);
end;

procedure SetBitToggle(var a : Integer; n : Integer);
begin
  a := a xor (1 shl n);
end;