Einzelnen Beitrag anzeigen

Benutzerbild von nicodex
nicodex

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

Re: Anzahl der Einträge in einem SET ermitteln

  Alt 3. Sep 2008, 13:47
Delphi-Quellcode:
{$RANGECHECKS OFF}
{$OVERFLOWCHECKS OFF}

function SidewaysAddition32(AValue: LongWord): LongWord; inline;
begin
  Result := AValue - ((AValue shr 1) and $55555555);
  Result := (Result and $33333333) + ((Result shr 2) and $33333333);
  Result := (Result + (Result shr 4)) and $0F0F0F0F;
  Result := (Result * $01010101) shr 24;
end;

function SidewaysAddition16(AValue: Word): Word; inline;
begin
  Result := AValue - ((AValue shr 1) and $5555);
  Result := (Result and $3333) + ((Result shr 2) and $3333);
  Result := (Result + (Result shr 4)) and $0F0F;
  Result := Word(Result * $0101) shr 8;
end;

function SidewaysAddition8(AValue: Byte): Byte; inline;
begin
  Result := AValue - ((AValue shr 1) and $55);
  Result := (Result and $33) + ((Result shr 2) and $33);
  Result := (Result + (Result shr 4)) and $0F;
end;

////////////////////////////////////////////////////////////////////////////////

procedure Foo();
type
  TBits8 = set of (bBit0, bBit1, bBit2, bBit3, bBit4, bBit5, bBit6, bBit7);
  TBits16 = set of (
    wBit0, wBit1, wBit2, wBit3, wBit4, wBit5, wBit6, wBit7,
    wBit8, wBit9, wBit10, wBit11, wBit12, wBit13, wBit14, wBit15
    );
  TBits32 = set of(
    dBit0, dBit1, dBit2, dBit3, dBit4, dBit5, dBit6, dBit7,
    dBit8, dBit9, dBit10, dBit11, dBit12, dBit13, dBit14, dBit15,
    dBit16, dBit17, dBit18, dBit19, dBit20, dBit21, dBit22, dBit23,
    dBit24, dBit25, dBit26, dBit27, dBit28, dBit29, dBit30, dBit31
    );
const
  Bar8: TBits8 = [bBit0, bBit3];
  Bar16: TBits16 = [wBit0];
  Bar32: TBits32 = [dBit16, dBit18, dBit31];
begin
  ShowMessage(
    IntToStr(SidewaysAddition8(Byte(Bar8))) + #13#10 +
    IntToStr(SidewaysAddition16(Word(Bar16))) + #13#10 +
    IntToStr(SidewaysAddition32(LongWord(Bar32)))
  );
end;
  Mit Zitat antworten Zitat