注:Delphi 的位操作(與或非)與邏輯操作的關鍵字是相同的。
位操作 | c/c++ | delphi/pascal |
與 | & | and |
或 | | | or |
非 | ! | not |
異或 | ^ | xor |
左移 | << | shl |
右移 | >> | shr |
c/c++中的邏輯操作轉Delphi表示方式 如:
c/c++
if(a&b) { // do something }
delphi
在c中關系運算只要是非0則為真,而在delphi中只有true/false,所以不能這么寫。
以下delphi寫法是錯誤的(無法編譯):
if a and b begin // do something end;
在delphi中應先進行位操作然后再進行關系判斷,以上表達式可以先 (a and b) 得到具體的數值,
然后再判斷是大于0、小于0、或者是等于0即可。如:
if (a and b) <> 0 begin // do something end;