Delphi in Boolean Type Analysis

zhaozj2021-02-17  82

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 from the perspective of resource occupation

A boolean type data takes up 1 byte memory;

A ByteBool type data takes up 1 byte of memory;

A WordBool type data takes up 2 bytes of memory;

A 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 type 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: / paste1.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 can compare the difference between reading data on this basis, you will have a more strange discovery.

Second, compare 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 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 in order, while the other three Boolean data types are disorderly. The following program gives some of these differences:

Procedure compareBooleanwithlongBool;

VAR

B: boolean;

Lb: longbool;

Begin

B: = FALSE;

Lb: = false;

If ORD (B) = ORD (LB) THENSHOWMESSAGE ('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) =' INTOSTR (ORD (B))); // must be 1

ShowMessage ('ORD (LB) =' INTOSTR (ORD (LB))); / / may be -1

END;

Excerpted from "CCID" / text

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

New Post(0)