AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Thema durchsuchen
Ansicht
Themen-Optionen

IsPowerOfTwo

Ein Thema von himitsu · begonnen am 6. Mai 2009 · letzter Beitrag vom 16. Aug 2009
Antwort Antwort
Seite 1 von 2  1 2      
Benutzerbild von himitsu
himitsu

Registriert seit: 11. Okt 2003
Ort: Elbflorenz
43.139 Beiträge
 
Delphi 12 Athens
 
#1

IsPowerOfTwo

  Alt 6. Mai 2009, 19:52
Hab hier noch was Kleines als Nachtrag

Code-Library -> Algorithmen -> IsPowerOfTwo

Delphi-Quellcode:
Function IsPowerOfTwo(i: LongWord): Boolean;
  ASM
    BSR EDX, EAX
    BSF EAX, EAX
    JZ @@None
    CMP EAX, EDX
    SETZ AL
    @@None:
  End;
es ist die Kurzfassung von:
Delphi-Quellcode:
Function IsPowerOfTwo(i: LongWord): Boolean;
  ASM
    TEST EAX, EAX
    JZ @@None
    BSR EDX, EAX
    BSF ECX, EAX
    CMP ECX, EDX
    JNE @@None
    MOV AL, &True
    @@None:
    MOV AL, &False
  End;

// bzw.

Function IsPowerOfTwo(i: LongWord): Boolean;
  ASM
    BSR EDX, EAX
    JZ @@None
    BSF ECX, EAX
    CMP ECX, EDX
    JNE @@None
    MOV AL, &True
    @@None:
    MOV AL, &False
  End;
Garbage Collector ... Delphianer erzeugen keinen Müll, also brauchen sie auch keinen Müllsucher.
my Delphi wish list : BugReports/FeatureRequests
  Mit Zitat antworten Zitat
Dax
(Gast)

n/a Beiträge
 
#2

Re: IsPowerOfTwo

  Alt 6. Mai 2009, 19:55
Lohnt sich das denn? Wenn man schon so weit geht, diese Dinge in Assembler zu schreiben - und damit Gegenüber dem Delphicompiler was gutmachen kann - wird der Call-Overhead doch so groß, dass es sich kaum noch lohnt
  Mit Zitat antworten Zitat
Benutzerbild von Desmulator
Desmulator

Registriert seit: 3. Mai 2007
Ort: Bonn
169 Beiträge
 
#3

Re: IsPowerOfTwo

  Alt 6. Mai 2009, 19:59
Zitat von Dax:
Lohnt sich das denn? Wenn man schon so weit geht, diese Dinge in Assembler zu schreiben - und damit Gegenüber dem Delphicompiler was gutmachen kann - wird der Call-Overhead doch so groß, dass es sich kaum noch lohnt Grübelnd...
Wenn man es als inline deklarieren würde, wie wärs dann?
Lars
There are 10 kinds of people in the world:
those who get binary, and those who don’t.
  Mit Zitat antworten Zitat
Benutzerbild von himitsu
himitsu

Registriert seit: 11. Okt 2003
Ort: Elbflorenz
43.139 Beiträge
 
Delphi 12 Athens
 
#4

Re: IsPowerOfTwo

  Alt 6. Mai 2009, 20:00
Kommt drauf an ... solange der Compiler keine Inline-Funktionen unterstützt (abgesehn davon, daß Hagens Funktion eh nicht als Inline definiert ist), kann man den Call-Overhead eh nicht umgehn.

[edit]
es ist auch mehr als Alternative zum ersten Code (mit dem Schleifchen) gedacht

das was die Schleife macht, übernimmt hier BSR bzw BSF



das Ganze ist 'ne angepaßte Auskopplung von
Delphi-Quellcode:
// RoL => rotate left
// RoR => rotate left
// OpenBit => number of highest bit (MSB)
// CloseBit => number of lowest bit (LSB)
// CountBits => number of bits
// OneBit => only one bit is set (OpenBit=CloseBit, IsPowerOfTwo)

{$DEFINE UseInline}

Function RoL( i: Byte; r: Byte): Byte; Overload;
Function RoL( i: Word; r: Byte): Word; Overload;
Function RoL( i: LongWord; r: Byte): LongWord; Overload;
Function RoL(Const i: LargeWord; r: Byte): LargeWord; Overload;
Function RoL( i: ShortInt; r: Byte): ShortInt; Overload;
Function RoL( i: SmallInt; r: Byte): SmallInt; Overload;
Function RoL( i: LongInt; r: Byte): LongInt; Overload;
Function RoL(Const i: LargeInt; r: Byte): LargeInt; Overload;

Function RoR( i: Byte; r: Byte): Byte; Overload;
Function RoR( i: Word; r: Byte): Word; Overload;
Function RoR( i: LongWord; r: Byte): LongWord; Overload;
Function RoR(Const i: LargeWord; r: Byte): LargeWord; Overload;
Function RoR( i: ShortInt; r: Byte): ShortInt; Overload;
Function RoR( i: SmallInt; r: Byte): SmallInt; Overload;
Function RoR( i: LongInt; r: Byte): LongInt; Overload;
Function RoR(Const i: LargeInt; r: Byte): LargeInt; Overload;

Function OpenBit( i: Byte): LongInt; Overload;
Function OpenBit( i: Word): LongInt; Overload;
Function OpenBit( i: LongWord): LongInt; Overload;
Function OpenBit(Const i: LargeWord): LongInt; Overload;
Function OpenBit( i: ShortInt): LongInt; Overload;
Function OpenBit( i: SmallInt): LongInt; Overload;
Function OpenBit( i: LongInt): LongInt; Overload;
Function OpenBit(Const i: LargeInt): LongInt; Overload;

Function CloseBit( i: Byte): LongInt; Overload;
Function CloseBit( i: Word): LongInt; Overload;
Function CloseBit( i: LongWord): LongInt; Overload;
Function CloseBit(Const i: LargeWord): LongInt; Overload;
Function CloseBit( i: ShortInt): LongInt; Overload;
Function CloseBit( i: SmallInt): LongInt; Overload;
Function CloseBit( i: LongInt): LongInt; Overload;
Function CloseBit(Const i: LargeInt): LongInt; Overload;

Function CountBits( i: Byte): LongInt; Overload;
Function CountBits( i: Word): LongInt; Overload;
Function CountBits( i: LongWord): LongInt; Overload;
Function CountBits(Const i: LargeWord): LongInt; Overload;
Function CountBits( i: ShortInt): LongInt; Overload; {$IFDEF UseInline}Inline;{$ENDIF}
Function CountBits( i: SmallInt): LongInt; Overload; {$IFDEF UseInline}Inline;{$ENDIF}
Function CountBits( i: LongInt): LongInt; Overload; {$IFDEF UseInline}Inline;{$ENDIF}
Function CountBits(Const i: LargeInt): LongInt; Overload; {$IFDEF UseInline}Inline;{$ENDIF}

Function OneBit( i: Byte): LongInt; Overload;
Function OneBit( i: Word): LongInt; Overload;
Function OneBit( i: LongWord): LongInt; Overload;
Function OneBit(Const i: LargeWord): LongInt; Overload;
Function OneBit( i: ShortInt): LongInt; Overload;
Function OneBit( i: SmallInt): LongInt; Overload;
Function OneBit( i: LongInt): LongInt; Overload;
Function OneBit(Const i: LargeInt): LongInt; Overload;



Function RoL(i: Byte; r: Byte): Byte;
  ASM
    MOV CL, &r
    ROL &i, CL
  End;

Function RoL(i: Word; r: Byte): Word;
  ASM
    MOV CL, &r
    ROL &i, CL
  End;

Function RoL(i: LongWord; r: Byte): LongWord;
  ASM
    MOV CL, &r
    ROL &i, CL
  End;

Function RoL(Const i: LargeWord; r: Byte): LargeWord;
  ASM
    MOVZX ECX, &r
    MOV EAX, DWORD PTR [&i]
    MOV EDX, DWORD PTR [&i + 4]
    CMP EAX, 0
    @@Loop:
    RCL EDX, 1
    RCL EAX, 1
    LOOP @@Loop
  End;

Function RoL(i: ShortInt; r: Byte): ShortInt;
  ASM
    MOV CL, &r
    ROL &i, CL
  End;

Function RoL(i: SmallInt; r: Byte): SmallInt;
  ASM
    MOV CL, &r
    ROL &i, CL
  End;

Function RoL(i: LongInt; r: Byte): LongInt;
  ASM
    MOV CL, &r
    ROL &i, CL
  End;

Function RoL(Const i: LargeInt; r: Byte): LargeInt;
  ASM
    MOV CL, &r
    MOV EAX, DWORD PTR [&i]
    MOV EDX, DWORD PTR [&i + 4]
    CMP EAX, 0
    @@Loop:
    RCL EDX, 1
    RCL EAX, 1
    DEC CL
    JNZ @@Loop
  End;

Function RoR(i: Byte; r: Byte): Byte;
  ASM
    MOV CL, &r
    ROR &i, CL
  End;

Function RoR(i: Word; r: Byte): Word;
  ASM
    MOV CL, &r
    ROR &i, CL
  End;

Function RoR(i: LongWord; r: Byte): LongWord;
  ASM
    MOV CL, &r
    ROR &i, CL
  End;

Function RoR(Const i: LargeWord; r: Byte): LargeWord;
  ASM
    MOV CL, &r
    MOV EAX, DWORD PTR [&i]
    MOV EDX, DWORD PTR [&i + 4]
    MOV ESI, EDX
    RCR ESI, 1
    @@Loop:
    RCR EAX, 1
    RCR EDX, 1
    DEC CL
    JNZ @@Loop
  End;

Function RoR(i: ShortInt; r: Byte): ShortInt;
  ASM
    MOV CL, &r
    ROR &i, CL
  End;

Function RoR(i: SmallInt; r: Byte): SmallInt;
  ASM
    MOV CL, &r
    ROR &i, CL
  End;

Function RoR(i: LongInt; r: Byte): LongInt;
  ASM
    MOV CL, &r
    ROR &i, CL
  End;

Function RoR(Const i: LargeInt; r: Byte): LargeInt;
  ASM
    MOV CL, &r
    MOV EAX, DWORD PTR [&i]
    MOV EDX, DWORD PTR [&i + 4]
    MOV ESI, EDX
    RCR ESI, 1
    @@Loop:
    RCR EAX, 1
    RCR EDX, 1
    DEC CL
    JNZ @@Loop
  End;

Function OpenBit(i: Byte): LongInt;
  ASM
    AND EAX, $000000ff
    MOV EDX, -1
    BSR EDX, EAX{&i}
    MOV EAX, EDX
  End;

Function OpenBit(i: Word): LongInt;
  ASM
    AND EAX, $0000ffff
    MOV EDX, -1
    BSR EDX, EAX{&i}
    MOV EAX, EDX
  End;

Function OpenBit(i: LongWord): LongInt;
  ASM
    MOV EDX, -1
    BSR EDX, &i
    MOV EAX, EDX
  End;

Function OpenBit(Const i: LargeWord): LongInt;
  ASM
    MOV EAX, -1
    BSR EAX, DWORD PTR [&i]
    JNZ @@Exit
    BSR EAX, DWORD PTR [&i + 4]
    JZ @@Exit
    ADD EAX, 32
    @@Exit:
  End;

Function OpenBit(i: ShortInt): LongInt;
  ASM
    AND EAX, $000000ff
    MOV EDX, -1
    BSR EDX, EAX{&i}
    MOV EAX, EDX
  End;

Function OpenBit(i: SmallInt): LongInt;
  ASM
    AND EAX, $0000ffff
    MOV EDX, -1
    BSR EDX, EAX{&i}
    MOV EAX, EDX
  End;

Function OpenBit(i: LongInt): LongInt;
  ASM
    MOV EDX, -1
    BSR EDX, &i
    MOV EAX, EDX
  End;

Function OpenBit(Const i: LargeInt): LongInt;
  ASM
    MOV EAX, -1
    BSR EAX, DWORD PTR [&i]
    JNZ @@Exit
    BSR EAX, DWORD PTR [&i + 4]
    JZ @@Exit
    ADD EAX, 32
    @@Exit:
  End;

Function CloseBit(i: Byte): LongInt;
  ASM
    AND EAX, $000000ff
    MOV EDX, -1
    BSF EDX, EAX{&i}
    MOV EAX, EDX
  End;

Function CloseBit(i: Word): LongInt;
  ASM
    AND EAX, $0000ffff
    MOV EDX, -1
    BSF EDX, EAX{&i}
    MOV EAX, EDX
  End;

Function CloseBit(i: LongWord): LongInt;
  ASM
    MOV EDX, -1
    BSF EDX, &i
    MOV EAX, EDX
  End;

Function CloseBit(Const i: LargeWord): LongInt;
  ASM
    MOV EAX, -1
    BSF EAX, DWORD PTR [&i + 4]
    JNZ @@Exit
    BSF EAX, DWORD PTR [&i]
    JZ @@Exit
    ADD EAX, 32
    @@Exit:
  End;

Function CloseBit(i: ShortInt): LongInt;
  ASM
    AND EAX, $000000ff
    MOV EDX, -1
    BSF EDX, EAX{&i}
    MOV EAX, EDX
  End;

Function CloseBit(i: SmallInt): LongInt;
  ASM
    AND EAX, $0000ffff
    MOV EDX, -1
    BSF EDX, EAX{&i}
    MOV EAX, EDX
  End;

Function CloseBit(i: LongInt): LongInt;
  ASM
    MOV EDX, -1
    BSF EDX, &i
    MOV EAX, EDX
  End;

Function CloseBit(Const i: LargeInt): LongInt;
  ASM
    MOV EAX, -1
    BSF EAX, DWORD PTR [&i + 4]
    JNZ @@Exit
    BSF EAX, DWORD PTR [&i]
    JZ @@Exit
    ADD EAX, 32
    @@Exit:
  End;

Function CountBits(i: Byte): LongInt;
  ASM
    MOV ECX, 8
    MOV DL, &i
    XOR EAX, EAX
    @@Loop:
    SHR EDX, 1
    JNC @@noInc
    INC EAX
    @@noInc:
    LOOP @@Loop
  End;

Function CountBits(i: Word): LongInt;
  ASM
    MOV ECX, 16
    MOV DX, &i
    XOR EAX, EAX
    @@Loop:
    SHR EDX, 1
    JNC @@noInc
    INC EAX
    @@noInc:
    LOOP @@Loop
  End;

Function CountBits(i: LongWord): LongInt;
  ASM
    MOV ECX, 32
    MOV EDX, &i
    XOR EAX, EAX
    @@Loop:
    SHR EDX, 1
    JNC @@noInc
    INC EAX
    @@noInc:
    LOOP @@Loop
  End;

Function CountBits(Const i: LargeWord): LongInt;
  ASM
    XOR EAX, EAX
    MOV ECX, 32
    MOV EDX, DWORD PTR [&i]
    @@Loop1:
    SHR EDX, 1
    JNC @@noInc1
    INC EAX
    @@noInc1:
    LOOP @@Loop1
    MOV ECX, 32
    MOV EDX, DWORD PTR [&i + 4]
    @@Loop2:
    SHR EDX, 1
    JNC @@noInc2
    INC EAX
    @@noInc2:
    LOOP @@Loop2
  End;

Function CountBits(i: ShortInt): LongInt;
  {$IFDEF UseInline}
    Begin
      Result := CountBits(Byte(i));
    End;
  {$ELSE}
    Const FD: Function(i: Byte): LongInt = CountBits;

    ASM
      JMP &FD
    End;
  {$ENDIF}

Function CountBits(i: SmallInt): LongInt;
  {$IFDEF UseInline}
    Begin
      Result := CountBits(Word(i));
    End;
  {$ELSE}
    Const FD: Function(i: Word): LongInt = CountBits;

    ASM
      JMP &FD
    End;
  {$ENDIF}

Function CountBits(i: LongInt): LongInt;
  {$IFDEF UseInline}
    Begin
      Result := CountBits(LongWord(i));
    End;
  {$ELSE}
    Const FD: Function(i: LongWord): LongInt = CountBits;

    ASM
      JMP &FD
    End;
  {$ENDIF}

Function CountBits(Const i: LargeInt): LongInt;
  {$IFDEF UseInline}
    Begin
      Result := CountBits(LargeWord(i));
    End;
  {$ELSE}
    Const FD: Function(Const i: LargeWord): LongInt = CountBits;

    ASM
      POP EBP
      JMP &FD
    End;
  {$ENDIF}

Function OneBit(i: Byte): LongInt;
  ASM
    AND EAX, $000000ff
    BSF EDX, EAX{&i}
    JZ @@None
    BSR ECX, EAX{&i}
    CMP ECX, EDX
    JNE @@None
    MOV EAX, EDX
    RET
    @@None:
    MOV EAX, -1
  End;

Function OneBit(i: Word): LongInt;
  ASM
    AND EAX, $0000ffff
    BSF EDX, EAX{&i}
    JZ @@None
    BSR ECX, EAX{&i}
    CMP ECX, EDX
    JNE @@None
    MOV EAX, EDX
    RET
    @@None:
    MOV EAX, -1
  End;

Function OneBit(i: LongWord): LongInt;
  ASM
    BSF EDX, &i
    JZ @@None
    BSR ECX, &i
    CMP ECX, EDX
    JNE @@None
    MOV EAX, EDX
    RET
    @@None:
    MOV EAX, -1
  End;

Function OneBit(Const i: LargeWord): LongInt;
  ASM
    BSF EAX, DWORD PTR [&i]
    JZ @@HiTest
    BSR EDX, DWORD PTR [&i]
    CMP EAX, EDX
    JNE @@None
    TEST DWORD PTR [&i + 8], 0
    JNE @@None
    TEST DWORD PTR [&i + 8], 0
    JNE @@None
    JMP @@Exit

    @@HiTest:
    BSF EAX, DWORD PTR [&i + 8]
    JZ @@None
    BSR EDX, DWORD PTR [&i + 8]
    CMP EAX, EDX
    JNE @@None
    JMP @@Exit

    @@None:
    MOV EAX, -1
    @@Exit:
  End;

Function OneBit(i: ShortInt): LongInt;
  ASM
    AND EAX, $000000ff
    BSF EDX, EAX{&i}
    JZ @@None
    BSR ECX, EAX{&i}
    CMP ECX, EDX
    JNE @@None
    MOV EAX, EDX
    RET
    @@None:
    MOV EAX, -1
  End;

Function OneBit(i: SmallInt): LongInt;
  ASM
    AND EAX, $0000ffff
    BSF EDX, EAX{&i}
    JZ @@None
    BSR ECX, EAX{&i}
    CMP ECX, EDX
    JNE @@None
    MOV EAX, EDX
    RET
    @@None:
    MOV EAX, -1
  End;

Function OneBit(i: LongInt): LongInt;
  ASM
    BSF EDX, &i
    JZ @@None
    BSR ECX, &i
    CMP ECX, EDX
    JNE @@None
    MOV EAX, EDX
    RET
    @@None:
    MOV EAX, -1
  End;

Function OneBit(Const i: LargeInt): LongInt;
  {$IFDEF UseInline}
    Begin
      Result := OneBit(LargeWord(i));
    End;
  {$ELSE}
    Const FD: Function(Const i: LargeInt): LongInt = OneBit;

    ASM
      POP EBP
      JMP &FD
    End;
  {$ENDIF}
Garbage Collector ... Delphianer erzeugen keinen Müll, also brauchen sie auch keinen Müllsucher.
my Delphi wish list : BugReports/FeatureRequests
  Mit Zitat antworten Zitat
gammatester

Registriert seit: 6. Dez 2005
999 Beiträge
 
#5

Re: IsPowerOfTwo

  Alt 7. Mai 2009, 08:07
Der einfachste (und schnellste?) Test ist wohl IsPowerOfTwo := (i<>0) and (i and pred(i) = 0); Gültig für alle Integer-Typen und vollständig ohne asm.


Gammatester
  Mit Zitat antworten Zitat
Benutzerbild von himitsu
himitsu

Registriert seit: 11. Okt 2003
Ort: Elbflorenz
43.139 Beiträge
 
Delphi 12 Athens
 
#6

Re: IsPowerOfTwo

  Alt 7. Mai 2009, 09:20
ist och hübsch und funktioniert ... entspricht auch Hagens 2. Version
Garbage Collector ... Delphianer erzeugen keinen Müll, also brauchen sie auch keinen Müllsucher.
my Delphi wish list : BugReports/FeatureRequests
  Mit Zitat antworten Zitat
Benutzerbild von 3_of_8
3_of_8

Registriert seit: 22. Mär 2005
Ort: Dingolfing
4.129 Beiträge
 
Turbo Delphi für Win32
 
#7

Re: IsPowerOfTwo

  Alt 7. Mai 2009, 09:34
Funktioniert aber so nicht bei negativen Zahlen - da müsste man ein i>0 reintun.
Manuel Eberl
„The trouble with having an open mind, of course, is that people will insist on coming along and trying to put things in it.“
- Terry Pratchett
  Mit Zitat antworten Zitat
Benutzerbild von himitsu
himitsu

Registriert seit: 11. Okt 2003
Ort: Elbflorenz
43.139 Beiträge
 
Delphi 12 Athens
 
#8

Re: IsPowerOfTwo

  Alt 7. Mai 2009, 09:39
Zitat von 3_of_8:
Funktioniert aber so nicht bei negativen Zahlen - da müsste man ein i>0 reintun.
ist klar, da durch die Dastellung oben alles 1 und unten alle Bits 0 sein müßten (siehe Aufbau des Zweierkomplements)

mit aktiver Bereichsprüfung
Delphi-Quellcode:
Function IsPowerOfTwo(i: Integer): Boolean;
  Begin
    Result := (i = Low(Integer))
      or (i > 0) and (i and Pred(i) = 0)
      or (i < 0) and (-i and Pred(-i) = 0);
  End;
ohne Bereichsprüfung
Delphi-Quellcode:
Function IsPowerOfTwo(i: Integer): Boolean;
  Begin
    Result := (i > 0) and (i and Pred(i) = 0)
      or (i < 0) and (-i and Pred(-i) = 0);
  End;
[edit]
Überlaufprüfung beachtet
oder man schaltet die Prüfung in der Funktion geziehlt um
Garbage Collector ... Delphianer erzeugen keinen Müll, also brauchen sie auch keinen Müllsucher.
my Delphi wish list : BugReports/FeatureRequests
  Mit Zitat antworten Zitat
Benutzerbild von mleyen
mleyen

Registriert seit: 10. Aug 2007
609 Beiträge
 
FreePascal / Lazarus
 
#9

Re: IsPowerOfTwo

  Alt 7. Mai 2009, 11:06
@himitsu:
Oben hast du Performancemäßig ja noch genau einen ASM-Befehl rausoptimiert.

Jetzt mit deiner Methode zur Prüfung auf negative Zahlen hast du bis zu 7 ASM-Befehle unnötig reingbracht.

Hier mal meine Lösung, die Performancemäßig am besten war:
Delphi-Quellcode:
function IsPowerOfTwoInl(const Value: Cardinal): Boolean; inline;
begin
  Result := (Value > 0) and (Value and (Value -1) = 0);
end;
//...
var
  value: Cardinal;
begin
  isPow2 := IsPowerOfTwoInl(value);
end;
Dieses ergibt 11 ASM-Befehle, wobei durch optimale Jumps minimal 6 Befehle durchlaufen werden.


Wenn man jetzt negative Zahlen testen will, soll man folgendes machen:
Delphi-Quellcode:
//...
var
  value: Integer;
begin
  isPow2 := IsPowerOfTwoInl(Abs(value));
end;
Diese ergibt 14 ASM-Befehle, wobei durch optimale Jumps minimal 7 Befehle durchlaufen werden.
(Dabei habe ich die standard Delphi abs()-Funktion genommen.)


Also negaH´s Funktion ist schon sehr schnell, wer aber noch mehr will, der sollte "inline" hinten dran hängen.
  Mit Zitat antworten Zitat
Benutzerbild von himitsu
himitsu

Registriert seit: 11. Okt 2003
Ort: Elbflorenz
43.139 Beiträge
 
Delphi 12 Athens
 
#10

Re: IsPowerOfTwo

  Alt 7. Mai 2009, 12:38
wie gesagt, die Bereichsprüfung sollte besser nicht unterschlagen werden (in ASM gibt's damit aber keine Probleme )

Delphi-Quellcode:
Function IsPowerOfTwo(i: Cardinal): Boolean; Inline;
  Begin
    {$IFOPT R+}
      {$R-}
        Result := (i <> 0) and (i and Pred(i) = 0);
      {$R+}
    {$ELSE}
      Result := (i <> 0) and (i and Pred(i) = 0);
    {$ENDIF}
  End;

// oder

Function IsPowerOfTwo(i: Cardinal): Boolean; Inline;
  Begin
    {$IFOPT R+}
      {$DEFINE IsPowerOfTwo_RangechecksOn}
      {$R-}
    {$ELSE}
      {$UNDEF IsPowerOfTwo_RangechecksOn}
    {$ENDIF}
    Result := (i <> 0) and (i and Pred(Abs(i)) = 0);
    {$IFDEF IsPowerOfTwo_RangechecksOn}
      {$R+}
    {$ENDIF}
  End;

// oder (wobei bei in meinen Codes _TEMP_ immer frei ist ... nicht daß man da mal ausversehn was überschreibt)

Function IsPowerOfTwo(i: Cardinal): Boolean; Inline;
  Begin
    {$IFOPT R+} {$R-} {$DEFINE _TEMP_} {$ELSE} {$UNDEF _TEMP_} {$ENDIF}
    Result := (i <> 0) and (i and Pred(Abs(i)) = 0);
    {$IFDEF _TEMP_} {$R+} {$ENDIF}
  End;
Delphi-Quellcode:
Function IsPowerOfTwo(i: Integer): Boolean; Inline;
  Begin
    {$IFOPT R+}
      {$R-}
        Result := (i <> 0) and (i and Pred(Abs(i)) = 0);
      {$R+}
    {$ELSE}
      Result := (i <> 0) and (i and Pred(Abs(i)) = 0);
    {$ENDIF}
  End;

// oder

Function IsPowerOfTwo(i: Integer): Boolean; Inline;
  Begin
    {$IFOPT R+}
      {$DEFINE IsPowerOfTwo_RangechecksOn}
      {$R-}
    {$ELSE}
      {$UNDEF IsPowerOfTwo_RangechecksOn}
    {$ENDIF}
    Result := (i <> 0) and (i and Pred(Abs(i)) = 0);
    {$IFDEF IsPowerOfTwo_RangechecksOn}
      {$R+}
    {$ENDIF}
  End;

// oder ...

Function IsPowerOfTwo(i: Integer): Boolean; Inline;
  Begin
    {$IFOPT R+} {$R-} {$DEFINE _TEMP_} {$ELSE} {$UNDEF _TEMP_} {$ENDIF}
    Result := (i <> 0) and (i and Pred(Abs(i)) = 0);
    {$IFDEF _TEMP_} {$R+} {$ENDIF}
  End;
Garbage Collector ... Delphianer erzeugen keinen Müll, also brauchen sie auch keinen Müllsucher.
my Delphi wish list : BugReports/FeatureRequests
  Mit Zitat antworten Zitat
Antwort Antwort
Seite 1 von 2  1 2      


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 16:01 Uhr.
Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz