![]() |
Was macht: "foo := ord(s[t] >= s[t+1]);" ?
Hallo,
- wie der Betreff schon andeutet: was macht foo := ord(s[t] >= s[t+1]); ? - wie sieht die IF-Klausel ausgeschrieben aus - falls es eine IF-Klausel ist ? |
AW: Was macht: "foo := ord(s[t] >= s[t+1]);" ?
Bei dem Schnipsel geht es weniger darum was er macht. Es mehr darum das der Code kryptisch wirkt und Leser zum Grübeln bringt, wie dich jetzt ;-)
Und nein, das hat nicht wirklich was mit IF zu tuen. In Delphi kommt da 0 oder 1 raus. Je nachdem was in s[t] und s[t+1] steht. |
AW: Was macht: "foo := ord(s[t] >= s[t+1]);" ?
Es vergleicht einfach zwei aufeinanderfolgende Items eines Strings/Arrays auf >=
und die Rückgabe ist eigentlich ein Boolean, der aber als Integer gespeichert ist, also 0 oder 1.
Delphi-Quellcode:
Achtung, je nach Boolean-Typ kann True auch -1 sein.
if s[t] >= s[t+1] then
foo := 1 // Ord(True) else foo := 0; // Ord(False) Im C++ ist die True-Konstante = -1 (alle Bits gesetzt) und im Delphi +1 (nur das kleinste Bit gesetzt) Boolean versus BOOL/ByteBool/WordBool/LongBool |
AW: Was macht: "foo := ord(s[t] >= s[t+1]);" ?
Danke für die Antwort.
|
AW: Was macht: "foo := ord(s[t] >= s[t+1]);" ?
Zitat:
den müsste man noch weg rechnen.
Delphi-Quellcode:
Function GE(const x:integer; const y:integer):integer; Inline;
Begin // geklaut hier https://codegolf.stackexchange.com/questions/226238/programming-less-than-greater-than-and-equal-to-functions-using-restricted- Result := (x - y + 1) div (abs(x - y) + 1); end; foo := GE(s[t],s[t+1]);
Delphi-Quellcode:
// BRANCHLESS COMPARE FUNCTIONS
Function BL_GE(const x,y:Integer):Integer;Inline; Begin Result := (x - y + 1) div (abs(x - y) + 1); End; Function BL_LE(const x,y:Integer):Integer;Inline; Begin Result := BL_GE(y,x); End; Function BL_E(const x,y:Integer):Integer;Inline; Begin Result := BL_GE(x, y) * BL_LE(x, y); End; Function BL_L(const x,y:Integer):Integer;Inline; Begin Result := 1 - BL_GE(x, y); // ODER BL_LE(x, y) - BL_E(x, y) End; Function BL_G(const x,y:Integer):Integer;Inline; Begin Result := 1 - BL_LE(x, y); // ODER BL_GE(x, y) - BL_E(x, y) End; |
Alle Zeitangaben in WEZ +1. Es ist jetzt 22:30 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz