Boolean type in Delphi

xiaoxiao2021-03-06  36

Four Boolean, Bytebool, WordBool, and longbool are defined in Delphi: Boolean, Bytebool, WordBool and LongBool. The three Boolean types are introduced in order to be compatible with other languages, in general, it is recommended to use the Boolean type.

The quantity of these four types of Boolean occupied memory is as follows:

Boolean 1 byte

Bytebool 1 byte

WordBool 2 Bytes (1 word)

Longbool 4 Bytes (2 Words)

For ByteBool, WordBool and longBool three types of TRUE constants are non-zero, false is zero, you can verify with the ORD function;

For the Boolean type, True constant is 1, FALSE is zero. In the context of the expected Boolean value, the compiler converts the three types of non-zero values ​​of ByteBool, WordBool, and longBool to True.

However, Bur Expressions and Integer / REAL are incompatible in Delphi. The following table compares Boolean and ByteBool / WordBool / LongBool in parallel:

Boolean

False

ORD (False) = 0

ORD (TRUE) = 1

SUCC (FALSE) = True

PRED (TRUE) = false

BoolTostr (TRUE) = -1 // This function is very metamorphosis

BoolTostr (false) = 0

Bytebool, WordBool, Longbool

False <> TRUE

ORD (False) = 0

ORD (TRUE) <> 0

SUCC (FALSE) = true

Pred (false) = true

BoolTostr This function is the most inexplicable, clearly said that True's value is 1, but it tells us that true is -1. The original shape of the function is:

Function BoolTostr (B: Boolean; UseBoolSTRS: Boolean = FALSE): STRING;

The following table is the conversion rule of the function:

B UseBoolSTRS Value of Returned String

TRUE FALSE '-1'

True True TrueBoolSTRS number of first values ​​(default, 'true ")

False False '0'

False True FalseBoolSTRS array first value (Default, 'false')

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

New Post(0)