| 類型 | 大小 | 順序性 | Ord(False) | Ord(True) | 用途 |
|---|---|---|---|---|---|
| Boolean | 1 Byte | 有序 | 0 | 1 | 首選,標準邏輯 |
| ByteBool | 1 Byte | 無序 | 0 | <>0 (通常 1) | C/C++ 相容 |
| WordBool | 2 Bytes | 無序 | 0 | <>0 | 16-bit API 相容 |
| LongBool | 4 Bytes | 無序 | 0 | <>0 (常為 -1) | 32-bit API/Windows 相容 |
Boolean:False < True,Succ(False)=True,Pred(True)=False。可用於陣列索引、集合運算。False <> True 但 無順序關係,Succ(False)=True、Pred(True)=False 但 Pred(False)=True (循環)。不可 作為陣列索引或集合元素。type
ByteBoolFile = file of ByteBool;
LongBoolFile = file of LongBool;
procedure CompareSize;
var BF: ByteBoolFile; LF: LongBoolFile; B: Boolean;
begin
B := False;
AssignFile(BF, 'test1.bin'); Rewrite(BF); Write(BF, B, B, B, B); CloseFile(BF); // 4 bytes
AssignFile(LF, 'test2.bin'); Rewrite(LF); Write(LF, B, B, B, B); CloseFile(LF); // 16 bytes
end;
同樣 4 個 False,檔案大小分別為 4 與 16 位元組。混用型別讀寫同一檔案會導致資料錯位。
var B: Boolean; LB: LongBool;
begin
B := True; LB := True;
ShowMessage(IntToStr(Ord(B))); // 永遠顯示 1
ShowMessage(IntToStr(Ord(LB))); // 可能顯示 -1 (視編譯器/平台)
end;
切勿假設 Ord(True) = 1 適用於所有布林類型,與 Windows API 交互時務必用 LongBool 對應 BOOL。
Boolean。BOOL 參數/回傳) 使用 LongBool 或直接用 LongBool 變數接收。ByteBool/WordBool/LongBool。Byte (0/1) 或明確指定型別,避免跨平台/版本差異。