There are four predefined Boolean types in Delphi: Boolean, ByteBool, WordBool, and LongBool. Among them, the Boolean type is the preferred Boolean type, and the remaining three provide compatibility support for other programming languages and Windows environments. These Boolean types are used in much the same way, but may have unexpected results if used interchangeably.
Here is a simple analysis for your reference.
1. Comparison from the perspective of resource usage
A piece of Boolean type data occupies 1 byte of memory;
A piece of ByteBool type data occupies 1 byte of memory;
A piece of WordBool type data occupies 2 bytes of memory;
A LongBool type data occupies 4 bytes of memory.
If the developer constructs a structure type containing a Boolean data type when designing a program, there will be considerations in terms of resource usage. Although these data types can be assigned to each other, there are differences in some special cases. First look at the following statement:
type
ByteBoolFile = file of ByteBool;
LongBoolFile = file of LongBool;
Here, if the same number of Boolean values are stored in these two types of files, their file sizes are different. When reading data from the same physical file according to these two types of files, the results are even more different.
The following is a program that compares ByteBool and LongBool. The file sizes of the obtained files test1.bin and test2.bin are 4 bytes and 16 bytes respectively.
PRocedure CompareByteBoolWithLongBool;
const
FName1 = 'c: est1.bin';
FName2 = 'c: est2.bin';
type
ByteBoolFile = file of ByteBool;
LongBoolFile = file of LongBool;
var
BF: ByteBoolFile;
LF: LongBoolFile;
B: Boolean;
begin
B := False;
AssignFile(BF, FName1);
Rewrite(BF);
Write(BF, B, B, B, B);
CloseFile(BF);
AssignFile(LF, FName2);
Rewrite(LF);
Write(LF, B, B, B, B);
CloseFile(LF);
end;
Interested friends can compare the differences in reading data on this basis, and you will find more peculiar discoveries.
2. Comparison from the perspective of Boolean value operations
In Delphi, Boolean values can only be assigned one of the predefined constants True and False. The above four Boolean data types have the following relationship:
Boolean ByteBool, WordBool, LongBool
False < True False <> True
Ord(False) = 0 Ord(False) = 0
Ord(True) = 1 Ord(True) <> 0
Succ(False) = True Succ(False) = True
Pred(True) = False Pred(False) = True
It is not difficult to see that the Boolean type is ordered, while the other three Boolean data types are unordered. The following program illustrates some of these differences:
procedure CompareBooleanWithLongBool;
var
B: Boolean;
LB: LongBool;
begin
B := False;
LB := False;
if Ord(B) = Ord(LB) then
ShowMessage('Ord(B) = Ord(LB) [B = LB = False]') //will be executed
else
ShowMessage('Ord(B) <> Ord(LB) [B = LB = False]');
B := True;
LB := True;
if Ord(B) = Ord(LB) then
ShowMessage('Ord(B) = Ord(LB) [B = LB = True]')
else
ShowMessage('Ord(B) <> Ord(LB) [B = LB = True]'); //will be executed
ShowMessage('Ord(B) = ' + IntToStr(Ord(B))); //Must be 1
ShowMessage('Ord(LB) = ' + IntToStr(Ord(LB))); //may be -1
end;