Sign in Join
Wiki copytemporary

Integer

Sent here from Int64.

Deutsch · English · Français · Italiano · Suomi · Русский

The data type integer is a built-in data type of the programming language Pascal. It can store a subset of ℤ, the set of whole numbers.

integer literal

basics

An integer is specified as a non-empty series of consecutive Western-Arabic digits.

pascal
1234

The value of 1234 is $1 \times 10^3 + 2 \times 10^2 + 3 \times 10^1 + 4 \times 10^0$. The integer may be preceded by a sign, + or −, even if mathematically speaking the value is signless (this concerns the value zero).

pascal
+0   { ✔ syntactically correct }

If no sign is specified, a positive sign is presumed.

In Free Pascal, you can use the underscore to group digits if {$modeSwitch underscoreIsSeparator+} (as of 2022 only available in trunk).

delphi
1_000_000_000

varying base

To change the base in Extended Pascal you prefix the integer literal with a base specification:

pascal
8#1234

This represents the value $1 \times 8^3 + 2 \times 8^2 + 3 \times 8^1 + 4 \times 8^0$. The base can be any value between 2 and 36 (inclusive) and can only be specified to a decimal base. A base specification possibly extends or restricts the set of allowed digits to those in the set of 0 to base − 1 from below table:

digit (case-insensitive) 0 1 2 3 4 5 6 7 8 9 A B
value (decimal) $0$ $1$ $2$ $3$ $4$ $5$ $6$ $7$ $8$ $9$ $10$ $11$
digit (case-insensitive) C D E F G H I J K L M N
value (decimal) $12$ $13$ $14$ $15$ $16$ $17$ $18$ $19$ $20$ $21$ $22$ $23$
digit (case-insensitive) O P Q R S T U V W X Y Z
value (decimal) $24$ $25$ $26$ $27$ $28$ $29$ $30$ $31$ $32$ $33$ $34$ $35$

integer digits in Extended Pascal

As of version 3.2.0, the FPC intends to ({$mode extendedPascal}), but does not yet support a generic base-specification format. Instead only the following bases are recognized:

base indicator sample (decimal value)
binary ($2$) % %1010 ($10$)
octal ($8$) & &644 ($420$)
decimal ($10$) none 1337 ($1337$)
hexadecimal ($16$) $ $2A ($42$)

integer base specifications in FPC 3.2.0

characteristics

It is guaranteed that all arithmetic operations in the range −maxInt..+maxInt work accurately. An integer variable may possibly store values beyond this range, but once you leave this range it is not guaranteed anymore that arithmetic operations work correctly. Textbook example:

pascal
program lordOverflowStrikesAgain(output);
    {$overflowChecks on}
    var
        x: integer;
    begin
        x := -maxInt;
        x := pred(x); { If this doesn’t cause an error, `-maxInt - 1` storable. }
        writeLn(abs(x));
    end.

Depending on the processor used, this may print:

text
-9223372036854775808

A quite unexpected result since abs should in principle return a non-negative value, yet expectable since pred(−maxInt) is evidently not in the −maxInt..+maxInt range.

application

Integer is the data of choice if arithmetic results have to be precise. The data type real may provide “reasonable approximations”, but operations on integer have to be exact (only guaranteed as long as it is in the −maxInt..+maxInt range). Generally speaking integer operations are also faster than if done in the domain of real.

The operators div and mod only work on integer values (the math unit provides the fMod function and overloads mod).

Free Pascal deviations

The FPC does not have a single data type integer but a host of integer data types. An integer literal such as 123 possesses the data type of closest fitting range from the following table.

name (aliases) smallest storable value largest storable value sizeOf
shortInt (int8) -128 ($-2^7$) 127 ($2^7-1$) 1
byte (uInt8) 0 ($0$) 255 ($2^8-1$) 1
smallInt (int16) -32768 ($-2^{15}$) 32767 ($2^{15}-1$) 2
word (uInt16) 0 ($0$) 65535 ($2^{16}-1$) 2
longInt (int32) -2147483648 ($-2^{31}$) 2147483647 ($2^{31}-1$) 4
longWord (cardinal, dWord) 0 ($0$) 4294967295 ($2^{32}-1$) 4
int64 -9223372036854775808 ($-2^{63}$) 9223372036854775807 ($2^{63}-1$) 8
qWord (uInt64) 0 ($0$) 18446744073709551615 ($2^{64}-1$) 8

integer data types in FPC version 3.2.0

The signed ranges are preferred (i. e. as in the top/down order in the table), thus 123 possesses the data type shortInt even though it could be a byte, too.

As of version 3.2.0, the data type integer is simply an alias depending on the currently selected compiler compatibility mode. It does not depend on the CPU type, therefore it is quite possible that the CPU could in fact deal with integers having an even larger magnitude than integer provides.

mode integer is an alias for value of maxInt
{$mode FPC}, {$mode macPas} and {$mode TP} smallInt 32767
all other available modes longInt 2147483647

the data type integer in FPC version 3.2.0

Warning: Undocumented feature: General programming advice, do not use what has not been documented. Unlikely as it may be, the following feature may be removed at any time.

Depending on the platform’s arithmetic logic unit’s word size (ALU) following aliases are available.

delphi
type
    {$ifDef CPU16} ALUSInt = smallInt; ALUUInt = word;  {$endIf}
    {$ifDef CPU32} ALUSInt = longInt;  ALUUInt = dWord; {$endIf}
    {$ifDef CPU64} ALUSInt = int64;    ALUUInt = qWord; {$endIf}

Although it is frequently the case that the ALU’s word size also coincides with the size of a pointer, it is not guaranteed (e. g. the x32-ABI uses 64‑bit ALU, but only 32‑bit pointers). Therefore if an integer value is meant to be typecasted to a pointer, it recommended to use ptrUInt. Note, the data type nativeInt is in fact an alias for ptrInt and not related to ALUSInt.

see also


From the Free Pascal wiki, page "Integer", as it stood on 2024-04-05 after a change by Mischi, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's Licensing page. If you have concerns about this material, message the admin.

Revision 1, last changed by WikiArchive .

Talk about this page

Nobody has opened a bench for this page yet. Sign in to open one.