Thema: Delphi IsPowerOfTwo

Einzelnen Beitrag anzeigen

Benutzerbild von himitsu
himitsu

Registriert seit: 11. Okt 2003
Ort: Elbflorenz
43.152 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