# Integer

(Redirected from Longint)
Jump to navigationJump to search

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.

1234


The value of 1234 is $\displaystyle{ 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).

+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). 1_000_000_000  ### varying base To change the base in Extended Pascal you prefix the integer literal with a base specification: 8#1234  This represents the value $\displaystyle{ 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) value (decimal) digit (case-insensitive) value (decimal) digit (case-insensitive) value (decimal) 0 1 2 3 4 5 6 7 8 9 A B $\displaystyle{ 0 }$ $\displaystyle{ 1 }$ $\displaystyle{ 2 }$ $\displaystyle{ 3 }$ $\displaystyle{ 4 }$ $\displaystyle{ 5 }$ $\displaystyle{ 6 }$ $\displaystyle{ 7 }$ $\displaystyle{ 8 }$ $\displaystyle{ 9 }$ $\displaystyle{ 10 }$ $\displaystyle{ 11 }$ C D E F G H I J K L M N $\displaystyle{ 12 }$ $\displaystyle{ 13 }$ $\displaystyle{ 14 }$ $\displaystyle{ 15 }$ $\displaystyle{ 16 }$ $\displaystyle{ 17 }$ $\displaystyle{ 18 }$ $\displaystyle{ 19 }$ $\displaystyle{ 20 }$ $\displaystyle{ 21 }$ $\displaystyle{ 22 }$ $\displaystyle{ 23 }$ O P Q R S T U V W X Y Z $\displaystyle{ 24 }$ $\displaystyle{ 25 }$ $\displaystyle{ 26 }$ $\displaystyle{ 27 }$ $\displaystyle{ 28 }$ $\displaystyle{ 29 }$ $\displaystyle{ 30 }$ $\displaystyle{ 31 }$ $\displaystyle{ 32 }$ $\displaystyle{ 33 }$ $\displaystyle{ 34 }$ $\displaystyle{ 35 }$ 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:

integer base specifications in FPC 3.2.0
base indicator sample (decimal value)
binary ($\displaystyle{ 2 }$) % %1010 ($\displaystyle{ 10 }$)
octal ($\displaystyle{ 8 }$) & &644 ($\displaystyle{ 420 }$)
decimal ($\displaystyle{ 10 }$) none 1337 ($\displaystyle{ 1337 }$)
hexadecimal ($\displaystyle{ 16 }$) $ $2A ($\displaystyle{ 42 }$)

## 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:

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: -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. integer data types in FPC version 3.2.0 name (aliases) smallest storable value largest storable value sizeOf shortInt (int8) -128 ($\displaystyle{ -2^7 }$) 127 ($\displaystyle{ 2^7-1 }$) 1 byte (uInt8) 0 ($\displaystyle{ 0 }$) 255 ($\displaystyle{ 2^8-1 }$) 1 smallInt (int16) -32768 ($\displaystyle{ -2^{15} }$) 32767 ($\displaystyle{ 2^{15}-1 }$) 2 word (uInt16) 0 ($\displaystyle{ 0 }$) 65535 ($\displaystyle{ 2^{16}-1 }$) 2 longInt (int32) -2147483648 ($\displaystyle{ -2^{31} }$) 2147483647 ($\displaystyle{ 2^{31}-1 }$) 4 longWord (cardinal, dWord) 0 ($\displaystyle{ 0 }$) 4294967295 ($\displaystyle{ 2^{32}-1 }$) 4 int64 -9223372036854775808 ($\displaystyle{ -2^{63} }$) 9223372036854775807 ($\displaystyle{ 2^{63}-1 }$) 8 qWord (uInt64) 0 ($\displaystyle{ 0 }$) 18446744073709551615 ($\displaystyle{ 2^{64}-1 }$) 8 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. the data type integer in FPC version 3.2.0 mode integer is an alias for value of maxInt {$mode FPC}, {$mode macPas} and {$mode TP} smallInt 32767
all other available modes longInt 2147483647

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.

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.