Например так:
Биты нумеруются справа с 0 по 31
Код:
function GetBit(Source : Cardinal; Position:Integer):Cardinal;
begin
Result := (Source shr Position) and 1;
end;
function GetNBit(Source : Cardinal; Position:Integer;Count:Integer):Cardinal;
begin
Result := (Source shl ( 31 - Position ));(* сдвигаем в лево, что бы ушли лишние старшие разряды *)
Result := Result shr( 31 - Position - Count -1 );(* сдвигаем в право, что бы ушли лишние младшие разряды *)
end;
procedure SetBit(var Source : Cardinal; Position:Integer; Bit:Byte);
begin
Mask:=1 shl Position;
Source := Source and not (1 shl Position); // обнуляем битик
Source := Source or ((Bit and 1) shl Position; // Записываем битик
end;
procedure SetNBit(var Source : Cardinal; Position,Count:Integer; Value:Cardinal);
var
MaskP: Cardinal;
MaskN: Cardinal;
begin
Value := Value shl (Position - Count -1);
MaskP := ((not 0) shl (32 - Count))shr (31 - Position);
MaskN:=not MaskP;
Source := (Source and MaskN) or (Value And maskP);
end;