Originale-mail to me for new edition

 

Integer types

 

An integer type represents a subset of the whole numbers. The generic integer types are Integer and Cardinal; use these whenever possible, since they result in the best performance for the underlying CPU and operating system. The table below gives their ranges and storage formats for the current 32-bit Object Pascal compiler.

 

Type

Range

Format

Integer

-2147483648..2147483647

signed 32-bit

Cardinal

0..4294967295

unsigned 32-bit

 

 

Fundamental integer types include Shortint, Smallint, Longint, Int64, Byte, Word, and Longword.

 

Type

Range

Format

Shortint

-128..127

signed 8-bit

Smallint

-32768..32767

signed 16-bit

Longint

-2147483648..2147483647

signed 32-bit

Int64

-2^63..2^63

signed 64-bit

Byte

0.255

unsigned 8-bit

Word

0.65535

unsigned 16-bit

Longword

0..4294967295

unsigned 32-bit

 

In general, arithmetic operations on integers return a value of type Integer which, in its current implementation, is equivalent to the 32-bit Longint. Operations return a value of type Int64 only when performed on an Int64 operand. Hence the following code produces incorrect results.

 

var

  I: Integer;

  J: Int64;

  ...

I := High(Integer);

J := I + 1;

 

To get an Int64 return value in this situation, cast I as Int64:

 

 ...

J := Int64(I) + 1;

 

For more information, see Arithmetic operators.

 

Note: Most standard routines that take integer arguments truncate Int64 values to 32 bits. However, the High, Low, Succ, Pred, Inc, Dec, IntToStr, and IntToHex routines fully support Int64 arguments. Also, the Round, Trunc, StrToInt64, and StrToInt64Def functions return Int64 values. A few routines including Ord cannot take Int64 values at all.

When you increment the last value or decrement the first value of an integer type, the result wraps around the beginning or end of the range. For example, the Shortint type has the range -128..127; hence, after execution of the code

 

var I: Shortint;

 ...

I := High(Shortint);

I := I + 1;

 

the value of I is -128. If compiler range-checking is enabled, however, this code generates a runtime error.

 

Topic groups

 

See also

Numerals

Ordinal types: Overview

Real types

 

 

译文

 

整数类型

 

整数类型表示全部数字的子界。一般的整数类型是IntegerCardinal,需要时,应当尽可能地使用这两种类型,因为它们在各种CPU和操作系统中都提供最佳的性能。下面是当前32Object Pascal编译器中这两种整数类型的范围和存储格式:

 

类型

范围

格式

Integer

-2147483648..2147483647

含符号的32

Cardinal

0..4294967295

无符号的32

 

基本整数类型包括ShortintSmallintLongintInt64ByteWordLongword等,如下:

 

类型

范围

格式

Shortint

-128..127

含符号的8

Smallint

-32768..32767

含符号的16

Longint

-2147483648..2147483647

含符号的32

Int64

-2^63..2^63

含符号的64

Byte

0.255

无符号的8

Word

0.65535

无符号的16

Longword

0..4294967295

无符号的32

 

通常,作用于整数的算术运算符返回Integer类型的值,在当前执行中,等价于32位的长整型(LongInt)。仅当对Int64类型执行运算时,运算结果返回Int64类型。因此,下面的代码将执行后得到的结果是不正确的:

 

var

  I: Integer;

  J: Int64;

  ...

I := High(Integer);

J := I + 1;

 

要使返回值是Int64类型,在上面的情况中可以将 I 转换为Int64

 

 ...

J := Int64(I) + 1;

 

更多信息见算术运算符

 

注意:大多数标准例程在处理Int64值的时候,都将参数截断为32位。不过,HighLowSuccPredIncDecIntToStrIntToHex等例程完全支持Int64参数。此外,RoundTruncStrToInt64StrToInt64Def等函数也可以返回Int64值。少数例程根本不能将Int64值作为参数,如Ord

对于整数类型,当要递增最后一个值或要递减第一个值的时候,运算结果将在范围的起点和中点之间环绕。例如,ShortInt类型的范围是 -128..127,因此,执行下面的代码:

 

var I: Shortint;

 ...

I := High(Shortint);

I := I + 1;

 

之后,变量 I 的值为 –128。如果范围检查编译指示有效({$R+},缺省为{$R-}),那么上面的代码将产生一个运行时错误。

 

主题组

 

相关主题

数字

序数类型:概述

实数类型