Delphi in Boolean Type Analysis

zhaozj2021-02-08  255

Delphi in Boolean Type Differentiation Suurus (2000-09-29)

The predefined Boolean type in Delphi has four: Boolean, Bytebool, WordBool, longbool. Among them, the Boolean type is the preferred Boolean type, the rest is to provide compatibility support for other programming languages ​​and Windows environments. These Boolean types are similar in terms of use, but if confusing will there be unexpected results. It is now simple and analyzed for your reference. First, compare a Boolean type data from the perspective of resource occupancy to occupy 1 byte memory; a ByteBool type data occupies 1 byte memory; a WordBool type data takes up 2 bytes of memory; LongBool type data takes up 4 bytes of memory. If the developer will construct a struct type containing the Boolean data type when performing programming, it will be considered in terms of resource occupation. Although these data types can be assigned each other, they are different in certain special circumstances. 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 files, their file size is different. The data is read by the two types of files, and the result is even more far. Below is a comparison of ByteBool and LongBool, the file size of the file TEST1.BIN and TEST2.BIN is 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); Closefile (BF); AssignFile (LF, FNAME2); REWRITE (LF); Write LF, B, B, B, B); Closefile (LF); END; Interested friends can compare the difference between reading data on this basis, you will have a more strange discovery. 2. Comparison from the operating angle of the Boolean value in Delphi, the 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 ORD (FALSE) = 0 ORD (FALSE) = 0 ORD (TRUE) = 1 Oord (TRUE) <> 0 SUCC False) = True Succ (false) = True Pred (true) = true PRED (false) = True is not difficult to see, the boolean type is ordered, and the other three Boolean data types are disorderly.

转载请注明原文地址:https://www.9cbs.com/read-1474.html

New Post(0)