Name
Cardinal Type
Syntax
type Cardinal = 0..4294967295;
Description
The Cardinal
type is an unsigned integer subrange whose size is the natural size
of an integer. In Delphi 5, the size is 32 bits, but in future
versions of Delphi, it might be larger. Use
LongWord
for an unsigned integer type that must be
32 bits, regardless of the natural size of an integer. See the
Integer
type for information about other integer
types.
Tips and Tricks
The most common use for
Cardinal
is calling Windows API or other external functions that take parameters of typeDWORD
(unsigned long in C or C++).If you need an integer type for natural or whole numbers, you should usually define your own subranges, as shown here:
// Better than Cardinal for use in computation type Whole = 1..MaxInt; Natural = 0..MaxInt;
Using
Cardinal
as an ordinary integer type often gives results different from what you expect because the result might be anyInteger
orCardinal
value. The range of values covered by each individual type is 32 bits, but the combination requires 33 bits. Thus, any arithmetic operation that combinesInteger
andCardinal
values forces the compiler to expand the operands to at least 33 bits—so Delphi converts the operands to theInt64
type:
type
I: Integer;
C: Cardinal; begin ReadLn(I, C); // Result of I+C can be Low(Integer)..High(Cardinal), which // requires 33 bits, so Delphi must use Int64 for the result type. WriteLn(I + C); // Comparing Integer and Cardinal requires changing I and C to // Int64 ...
Get Delphi in a Nutshell now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.