function CTypeShr(Value: Byte; BitCount: Integer): Byte; overload;
begin
Result := Value shr BitCount;
end;
function CTypeShr(Value: Word; BitCount: Integer): Word; overload;
begin
Result := Value shr BitCount;
end;
function CTypeShr(Value: LongWord; BitCount: Integer): LongWord; overload;
begin
Result := Value shr BitCount;
end;
function CTypeShr(Value: ShortInt; BitCount: Integer): ShortInt; overload;
var
Sign: ShortInt;
ShiftMask: ShortInt;
Count: Integer;
begin
Result := Value shr BitCount;
if (Value < 0) then
begin
Sign := ShortInt($80);
ShiftMask := 0;
for Count := 1 to BitCount do
begin
ShiftMask := ShiftMask or Sign;
Sign := Sign shr 1;
end;
Result := ShiftMask or Result;
end;
end;
function CTypeShr(Value: SmallInt; BitCount: Integer): SmallInt; overload;
var
Sign: SmallInt;
ShiftMask: SmallInt;
Count: Integer;
begin
Result := Value shr BitCount;
if (Value < 0) then
begin
Sign := SmallInt($8000);
ShiftMask := 0;
for Count := 1 to BitCount do
begin
ShiftMask := ShiftMask or Sign;
Sign := Sign shr 1;
end;
Result := ShiftMask or Result;
end;
end;
function CTypeShr(Value: LongInt; BitCount: Integer): LongInt; overload;
var
Sign: LongInt;
ShiftMask: LongInt;
Count: Integer;
begin
Result := Value shr BitCount;
if (Value < 0) then
begin
Sign := LongInt($80000000);
ShiftMask := 0;
for Count := 1 to BitCount do
begin
ShiftMask := ShiftMask or Sign;
Sign := Sign shr 1;
end;
Result := ShiftMask or Result;
end;
end;
델파이의 비트연산자에는 shl, shr, or, and, xor등이 있습니다...
C의 Shift Right연산은 Signed와 Unsigned가 달라요...
때문에 아래와 같은 식으로 함수 만드셔서 하세요...
function CTypeShr(Value: Byte; BitCount: Integer): Byte; overload;
begin
Result := Value shr BitCount;
end;
function CTypeShr(Value: Word; BitCount: Integer): Word; overload;
begin
Result := Value shr BitCount;
end;
function CTypeShr(Value: LongWord; BitCount: Integer): LongWord; overload;
begin
Result := Value shr BitCount;
end;
function CTypeShr(Value: ShortInt; BitCount: Integer): ShortInt; overload;
var
Sign: ShortInt;
ShiftMask: ShortInt;
Count: Integer;
begin
Result := Value shr BitCount;
if (Value < 0) then
begin
Sign := ShortInt($80);
ShiftMask := 0;
for Count := 1 to BitCount do
begin
ShiftMask := ShiftMask or Sign;
Sign := Sign shr 1;
end;
Result := ShiftMask or Result;
end;
end;
function CTypeShr(Value: SmallInt; BitCount: Integer): SmallInt; overload;
var
Sign: SmallInt;
ShiftMask: SmallInt;
Count: Integer;
begin
Result := Value shr BitCount;
if (Value < 0) then
begin
Sign := SmallInt($8000);
ShiftMask := 0;
for Count := 1 to BitCount do
begin
ShiftMask := ShiftMask or Sign;
Sign := Sign shr 1;
end;
Result := ShiftMask or Result;
end;
end;
function CTypeShr(Value: LongInt; BitCount: Integer): LongInt; overload;
var
Sign: LongInt;
ShiftMask: LongInt;
Count: Integer;
begin
Result := Value shr BitCount;
if (Value < 0) then
begin
Sign := LongInt($80000000);
ShiftMask := 0;
for Count := 1 to BitCount do
begin
ShiftMask := ShiftMask or Sign;
Sign := Sign shr 1;
end;
Result := ShiftMask or Result;
end;
end;
begin
CTypeShr(Data and $F0, 4) + $30;
end;
^^ 항상 즐코하세요...