Einzelnen Beitrag anzeigen

Benutzerbild von nicodex
nicodex

Registriert seit: 2. Jan 2008
Ort: Darmstadt
286 Beiträge
 
Delphi 2007 Professional
 
#3

Re: Zugriff auf einzelne bits einer Variable

  Alt 15. Apr 2008, 10:45
Was genau willst du machen?

ps: Es gibt die Möglichkeit die Bits über ein Set abzubilden, sie über Operatoren zu lesen/manipulieren, oder die Klasse TBits zu verwenden...
edit: "(Variable and 8) = 8" ist langsamer als "(Variable and 8) <> 0".

Delphi-Quellcode:
type
  PByteBit = ^TByteBit;
  TByteBit = (bBit0, bBit1, bBit2, bBit3, bBit4, bBit5, bBit6, bBit7);
  PByteBits = ^TByteBits;
  TByteBits = set of TByteBit;

function TestBit(ABits: TByteBits; ABit: TByteBit): Boolean;
{$IFDEF SUPPORTS_INLINE} inline; {$ENDIF}
begin
  Result := ABit in ABits;
end;

procedure Foo();
var
  Bar: Byte;
begin
  Bar := 42;
  if TestBit(TByteBits(Bar), bBit1) then
    ShowMessage('Hello, World!');
end;
Nico Bendlin
  Mit Zitat antworten Zitat