[fpc-pascal] Bit manipulation
Jonas Raoni
jonasraoni at gmail.com
Mon Feb 20 17:44:19 CET 2006
On 2/20/06, Pianoman <pianoman at centrum.sk> wrote:
> Hi, I need to know how can I see whether a certain bit of byte/word/dword is
> set: or not:
> function like this would be appreciated:
> function isset(value: byte word or dword;bit:byte):boolean;
> for example when i pass the function parrameter a of type word and I want to
> see whether the bit 14 is set.
function GetBit(const Value: DWord; const Bit: Byte): Boolean;
begin
Result := (Value and (1 shl Bit)) <> 0;
end;
function ClearBit(const Value: DWord; const Bit: Byte): DWord;
begin
Result := Value and not (1 shl Bit);
end;
function SetBit(const Value: DWord; const Bit: Byte): DWord;
begin
Result := Value or (1 shl Bit);
end;
function EnableBit(const Value: DWord; const Bit: Byte; const TurnOn:
Boolean): DWord;
begin
Result := (Value or (1 shl Bit)) xor (Integer(not TurnOn) shl Bit);
end;
--
Jonas Raoni Soares Silva
http://www.jsfromhell.com
More information about the fpc-pascal
mailing list