There are four kinds of predefined Boolean types in Delphi: Boolean, Bytebool,wordbool,longbool. Where the Boolean type is the preferred Boolean type, the remaining three provide compatibility support for other programming languages and Windows environments. These Boolean types are similar in use, but may have unintended consequences if used in confusion.
Now do a simple analysis for everyone's reference.
I. Comparison from the point of view of resource occupancy
A Boolean type of data consumes 1 bytes of memory;
A bytebool type of data consumes 1 bytes of memory;
A wordbool type of data consumes 2 bytes of memory;
A longbool type of data consumes 4 bytes of memory.
If a developer constructs a struct type that contains a Boolean data type when designing a program, the resource footprint will be considered. Although these data types can be assigned to each other, there are special cases where there is a difference. First look at the following statement:
Type
Byteboolfile = file of Bytebool;
Longboolfile = file of Longbool;
Here, if you store the same number of Boolean values in both types of files, their file sizes are different. And the same physical file according to the two types of files read data separately, the result is much different.
The following is a program that compares Bytebool and Longbool, and the resulting file Test1.bin and Test2.bin file sizes are 4 bytes and 16 bytes respectively.
procedure CompareByteBoolWithLongBool;
const
FName1 = 'c:test1.bin';
FName2 = 'c:test2.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 on this basis to compare the difference between reading data, you will have more exotic discoveries.
Second, from the operational point of view of the Boolean comparison
In Delphi, a Boolean value can only be given one of the predefined constants true and false. The above four Boolean data types have the following relationships:
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 gives some of the 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]') //将被执行
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]'); //将被执行
ShowMessage('Ord(B) = ' + IntToStr(Ord(B))); //一定是 1
ShowMessage('Ord(LB) = ' + IntToStr(Ord(LB))); //可能是-1
end;