Den Vorschlag von Aviator aufgenommen und erweitert:
Delphi-Quellcode:
program Project5;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils;
function CheckBit(
const Value, BitNumber: Integer): Integer;
var
i: Integer;
Mask: Integer;
begin
if (Value <> 0)
then
begin
i := 0;
Mask := 0;
while true
do
begin
if Mask = 0
then
Mask := 1;
if (Value
and Mask) <> 0
then
begin
Inc(i);
if (i = BitNumber)
then
begin
Result := Mask;
Break;
end;
end;
Mask := Mask
shl 1;
end;
end
else
Result := -1;
end;
begin
try
if CheckBit($2A, 3) = 32
then
writeln('
Test 1 - ok');
if CheckBit($2A, 5) = 8
then
writeln('
Test 2 - ok');
readln;
except
on E:
Exception do
writeln(E.ClassName, '
: ', E.
Message);
end;
end.