Einzelnen Beitrag anzeigen

Benutzerbild von nicodex
nicodex

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

Re: Anzahl der Einträge in einem SET ermitteln

  Alt 3. Sep 2008, 13:16
Im Endeffekt willst du die Bits zählen. Dafür gibt es Optimierungen...

Delphi-Quellcode:
//
// A generalization of the best bit counting method (sideways addition)
// to integers of bit widths upto 128 (parameterized by type T) is this:
//
// v := v - ((v shr 1) and T(not T(0) div 3));
// v := (v and T(not T(0) div 15 * 3)) + ((v shr 2) and T(not T(0) div 15 * 3));
// v := (v + (v shr 4)) and T(not T(0) div 255 * 15);
// c := T(v * T(not T(0) div 255)) shr ((SizeOf(T) - 1) * ByteBits);
//

// 32-bit
Val32 := PLongWord(Data)^;
Val32 := Val32 - ((Val32 shr 1) and $55555555);
Val32 := (Val32 and $33333333) + ((Val32 shr 2) and $33333333);
Val32 := (Val32 + (Val32 shr 4)) and $0F0F0F0F;
Val32 := (Val32 * $01010101) shr 24;
// 16-bit
Val16 := PWord(Data)^;
Val16 := Val16 - ((Val16 shr 1) and $5555);
Val16 := (Val16 and $3333) + ((Val16 shr 2) and $3333);
Val16 := (Val16 + (Val16 shr 4)) and $0F0F;
Val16 := Word(Val16 * $0101) shr 8;
// 8-bit
Val08 := Data^;
Val08 := Val08 - ((Val08 shr 1) and $55);
Val08 := (Val08 and $33) + ((Val08 shr 2) and $33);
Val08 := (Val08 + (Val08 shr 4)) and $0F;
Quelle: http://graphics.stanford.edu/~seande...ntBitsSetNaive
  Mit Zitat antworten Zitat