Integer (computer science)
In computer science, an integer is a
Value and representation
The value of an item with an integral type is the mathematical integer that it corresponds to. Integral types may be unsigned (capable of representing only non-negative integers) or signed (capable of representing negative integers as well).[1]
An integer value is typically specified in the
The internal representation of this datum is the way the value is stored in the computer's memory. Unlike mathematical integers, a typical datum in a computer has some minimal and maximum possible value.
The most common representation of a positive integer is a string of
There are four well-known
Some computer languages define integer sizes in a machine-independent way; others have varying definitions depending on the underlying processor word size. Not all language implementations define variables of all integer sizes, and defined sizes may not even be distinct in a particular implementation. An integer in one programming language may be a different size in a different language, on a different processor, or in an execution context of different bitness; see § Words.
Some older computer architectures used decimal representations of integers, stored in binary-coded decimal (BCD) or other format. These values generally require data sizes of 4 bits per decimal digit (sometimes called a nibble), usually with additional bits for a sign. Many modern CPUs provide limited support for decimal integers as an extended datatype, providing instructions for converting such values to and from binary values. Depending on the architecture, decimal integers may have fixed sizes (e.g., 7 decimal digits plus a sign fit into a 32-bit word), or may be variable-length (up to some maximum digit size), typically occupying two digits per byte (octet).
Common integral data types
Bits | Name | Range (assuming two's complement for signed) | Decimal digits | Uses | Implementations | |||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
C/C++ | C# | Delphi
|
Java | SQL[a] | FORTRAN | D | Rust | |||||
4 | nibble, semioctet | Signed: From −8 to 7, from −(23) to 23 − 1 | 0.9 | Binary-coded decimal, single decimal digit representation | — | — | — | — | — | — | — | — |
Unsigned: From 0 to 15, which equals 24 − 1 | 1.2 | |||||||||||
8 | byte, octet, i8, u8 | Signed: From −128 to 127, from −(27) to 27 − 1 | 2.11 | code units in the UTF-8 character encoding
|
int8_t, signed char[b] | sbyte | Shortint | byte | tinyint | integer(1) | byte | i8 |
Unsigned: From 0 to 255, which equals 28 − 1 | 2.41 | uint8_t, unsigned char[b] | byte | Byte | — | unsigned tinyint | — | ubyte | u8 | |||
16 | halfword, word , short, i16, u16
|
Signed: From −32,768 to 32,767, from −(215) to 215 − 1 | 4.52 | code units in the UTF-16 character encoding
|
int16_t, short[b], int[b] | short | Smallint | short | smallint | integer(2) | short | i16 |
Unsigned: From 0 to 65,535, which equals 216 − 1 | 4.82 | uint16_t, unsigned[b], unsigned int[b] | ushort | Word | char[c] | unsigned smallint | — | ushort | u16 | |||
32 | word, long , doubleword, longword, int, i32, u32
|
Signed: From −2,147,483,648 to 2,147,483,647 , from −(231) to 231 − 1
|
9.33 | true color with alpha, FourCC, pointers in 32-bit computing
|
int32_t, int[b], long[b] | int | LongInt; Integer[d] | int | int | integer(4) | int | i32 |
Unsigned: From 0 to 4,294,967,295, which equals 232 − 1 | 9.63 | uint32_t, unsigned[b], unsigned int[b], unsigned long[b] | uint | LongWord; DWord; Cardinal[d] | — | unsigned int | — | uint | u32 | |||
64 | word, doubleword, longword, long, long long, quad, quadword, qword, int64, i64, u64 | Signed: From −9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 , from −(263) to 263 − 1
|
18.96 | Time (milliseconds since the Unix epoch), pointers in 64-bit computing
|
int64_t, long[b], long long[b] | long | Int64 | long | bigint | integer(8) | long | i64 |
Unsigned: From 0 to 18,446,744,073,709,551,615, which equals 264 − 1 | 19.27 | uint64_t, unsigned long long[b] | ulong | UInt64; QWord | — | unsigned bigint | — | ulong | u64 | |||
128 | octaword, double quadword, i128, u128 | Signed: From −170,141,183,460,469,231,731,687,303,715,884,105,728 to 170,141,183,460,469,231,731,687,303,715,884,105,727, from −(2127) to 2127 − 1 | 38.23 | Complex scientific calculations,
IPv6 addresses, GUIDs
|
C: only available as non-standard compiler-specific extension | — | — | — | — | integer(16) | cent[e] | i128 |
Unsigned: From 0 to 340,282,366,920,938,463,463,374,607,431,768,211,455, which equals 2128 − 1 | 38.53 | — | ucent[e] | u128 | ||||||||
n | n-bit integer (general case) |
Signed: −(2n−1) to (2n−1 − 1) | (n − 1) log10 2 | C23: _BitInt(n), signed _BitInt(n) | Ada: range -2**(n-1)..2**(n-1)-1 | |||||||
Unsigned: 0 to (2n − 1) | n log10 2 | C23: unsigned _BitInt(n) | Ada: range 0..2**n-1, mod 2**n; standard libraries' or third-party arbitrary arithmetic libraries' BigDecimal or Decimal classes in many languages such as Python, C++, etc. |
Different CPUs support different integral data types. Typically, hardware will support both signed and unsigned types, but only a small, fixed set of widths.
The table above lists integral type widths that are supported in hardware by common processors. High level programming languages provide more possibilities. It is common to have a 'double width' integral type that has twice as many bits as the biggest hardware-supported type. Many languages also have bit-field types (a specified number of bits, usually constrained to be less than the maximum hardware-supported width) and range types (that can represent only the integers in a specified range).
Some languages, such as
A
A four-bit quantity is known as a nibble (when eating, being smaller than a bite) or nybble (being a pun on the form of the word byte). One nibble corresponds to one digit in hexadecimal and holds one digit or a sign code in binary-coded decimal.
Bytes and octets
The term byte initially meant 'the smallest addressable unit of memory'. In the past, 5-, 6-, 7-, 8-, and 9-bit bytes have all been used. There have also been computers that could address individual bits ('bit-addressed machine'), or that could only address 16- or 32-bit quantities ('word-addressed machine'). The term byte was usually not used at all in connection with bit- and word-addressed machines.
The term octet always refers to an 8-bit quantity. It is mostly used in the field of computer networking, where computers with different byte widths might have to communicate.
In modern usage byte almost invariably means eight bits, since all other sizes have fallen into disuse; thus byte has come to be synonymous with octet.
Words
The term 'word' is used for a small group of bits that are handled simultaneously by processors of a particular architecture. The size of a word is thus CPU-specific. Many different word sizes have been used, including 6-, 8-, 12-, 16-, 18-, 24-, 32-, 36-, 39-, 40-, 48-, 60-, and 64-bit. Since it is architectural, the size of a word is usually set by the first CPU in a family, rather than the characteristics of a later compatible CPU. The meanings of terms derived from word, such as longword, doubleword, quadword, and halfword, also vary with the CPU and OS.[7]
Practically all new desktop processors are capable of using 64-bit words, though
One important cause of non-portability of software is the incorrect assumption that all computers have the same word size as the computer used by the programmer. For example, if a programmer using the C language incorrectly declares as int a variable that will be used to store values greater than 215−1, the program will fail on computers with 16-bit integers. That variable should have been declared as long, which has at least 32 bits on any computer. Programmers may also incorrectly assume that a pointer can be converted to an integer without loss of information, which may work on (some) 32-bit computers, but fail on 64-bit computers with 64-bit pointers and 32-bit integers. This issue is resolved by C99 in
intptr_t
.
The bitness of a program may refer to the word size (or bitness) of the processor on which it runs, or it may refer to the width of a memory address or pointer, which can differ between execution modes or contexts. For example, 64-bit versions of Microsoft Windows support existing 32-bit binaries, and programs compiled for Linux's x32 ABI run in 64-bit mode yet use 32-bit memory addresses.[8]
Standard integer
The standard integer size is platform-dependent.
In C, it is denoted by int and required to be at least 16 bits. Windows and Unix systems have 32-bit ints on both 32-bit and 64-bit architectures.
Short integer
A short integer can represent a whole number that may take less storage, while having a smaller range, compared with a standard integer on the same machine.
In C, it is denoted by short. It is required to be at least 16 bits, and is often smaller than a standard integer, but this is not required.[9][10] A conforming program can assume that it can safely store values between −(215−1)[11] and 215−1,[12] but it may not assume that the range is not larger. In Java, a short is always a 16-bit integer. In the Windows API, the datatype SHORT is defined as a 16-bit signed integer on all machines.[7]
Programming language | Data type name | Signedness | Size in bytes
|
Minimum value | Maximum value |
---|---|---|---|---|---|
C and C++ | short | signed | 2 | −32,767[f] | +32,767 |
unsigned short | unsigned | 2 | 0 | 65,535 | |
C# | short | signed | 2 | −32,768 | +32,767 |
ushort | unsigned | 2 | 0 | 65,535 | |
Java | short | signed | 2 | −32,768 | +32,767 |
SQL | smallint | signed | 2 | −32,768 | +32,767 |
Long integer
A long integer can represent a whole
In C, it is denoted by long. It is required to be at least 32 bits, and may or may not be larger than a standard integer. A conforming program can assume that it can safely store values between −(231−1)[11] and 231−1,[12] but it may not assume that the range is not larger.
Programming language | Approval Type | Platforms | Data type name | Storage in bytes
|
Signed range | Unsigned range |
---|---|---|---|---|---|---|
C ISO/ANSI C99
|
International Standard | Windows, 16/32/64-bit systems[7]
|
long | 4 (minimum requirement 4) |
−2,147,483,647 to +2,147,483,647 | 0 to 4,294,967,295 (minimum requirement) |
C ISO/ANSI C99
|
International Standard | Unix, 64-bit systems[7][10] |
long | 8 (minimum requirement 4) |
−9,223,372,036,854,775,807 to +9,223,372,036,854,775,807 | 0 to 18,446,744,073,709,551,615 |
C++ ISO/ANSI | International Standard | Windows ,16/32-bit system |
long | 4 [13] (minimum requirement 4) |
−2,147,483,648 to +2,147,483,647 |
0 to 4,294,967,295 (minimum requirement) |
C++/CLI | International Standard ECMA-372
|
Windows ,16/32-bit systems |
long | 4 [14] (minimum requirement 4) |
−2,147,483,648 to +2,147,483,647 |
0 to 4,294,967,295 (minimum requirement) |
VB | Company Standard | Windows | Long | 4 [15] | −2,147,483,648 to +2,147,483,647 | — |
VBA | Company Standard | Mac OS X
|
Long | 4 [16] | −2,147,483,648 to +2,147,483,647 | — |
SQL Server | Company Standard | Windows | BigInt | 8 | −9,223,372,036,854,775,808 to +9,223,372,036,854,775,807 | 0 to 18,446,744,073,709,551,615 |
VB.NET
|
ECMA International Standard | Microsoft .NET
|
long or Int64 | 8 | −9,223,372,036,854,775,808 to +9,223,372,036,854,775,807 | 0 to 18,446,744,073,709,551,615 |
Java | International/Company Standard | Java platform
|
long | 8 | −9,223,372,036,854,775,808 to +9,223,372,036,854,775,807 | — |
Pascal | ? | UNIX
|
int64 | 8 | −9,223,372,036,854,775,808 to +9,223,372,036,854,775,807 | 0 to 18,446,744,073,709,551,615 (Qword type) |
Long long
In the
Syntax
Literals for integers can be written as regular
42
10000
-233000
There are several alternate methods for writing integer literals in many programming languages:
- Many programming languages, especially those influenced by C, prefix an integer literal with
0X
or0x
to represent a hexadecimal value, e.g.0xDEADBEEF
. Other languages may use a different notation, e.g. some assembly languages append anH
orh
to the end of a hexadecimal value. - Perl, Ruby, Java, Julia, D, Go, Rust and Python (starting from version 3.6) allow embedded underscores for clarity, e.g.
10_000_000
, and fixed-form Fortran ignores embedded spaces in integer literals. C (starting from C23) and C++ use single quotes for this purpose. - In Unix modes; however, it has been criticized because normal integers may also lead with zero.[19] As such, Python, Ruby, Haskell, and OCamlprefix octal values with
0O
or0o
, following the layout used by hexadecimal values. - Several languages, including Java, C#, Scala, Python, Ruby, OCaml, C (starting from C23) and C++ can represent binary values by prefixing a number with
0B
or0b
.
See also
- Arbitrary-precision arithmetic
- Binary-coded decimal (BCD)
- C data types
- Integer overflow
- Signed number representations
Notes
- ^ Not all SQL dialects have unsigned datatypes.[4][5]
- ^ a b c d e f g h i j k l m n The sizes of char, short, int, long and long long in C/C++ are dependent upon the implementation of the language.
- ^ Java does not directly support arithmetic on char types. The results must be cast back into char from an int.
- ^ a b The sizes of Delphi's Integer and Cardinal are not guaranteed, varying from platform to platform; usually defined as LongInt and LongWord respectively.
- ^ a b Reserved for future use. Not implemented yet.
- ^ The ISO C standard allows implementations to reserve the value with sign bit 1 and all other bits 0 (for sign–magnitude and two's complement representation) or with all bits 1 (for ones' complement) for use as a "trap" value, used to indicate (for example) an overflow.[11]
References
- ^ Cheever, Eric. "Representation of numbers". Swarthmore College. Retrieved 2011-09-11.
- ^ Madhusudhan Konda (2011-09-02). "A look at Java 7's new features - O'Reilly Radar". Radar.oreilly.com. Retrieved 2013-10-15.
- ISBN 978-0-262-34821-8.
- ^ "Sybase Adaptive Server Enterprise 15.5: Exact Numeric Datatypes".
- ^ "MySQL 5.6 Numeric Datatypes".
- ^ "BigInteger (Java Platform SE 6)". Oracle. Retrieved 2011-09-11.
- ^ a b c d e Fog, Agner (2010-02-16). "Calling conventions for different C++ compilers and operating systems: Chapter 3, Data Representation" (PDF). Retrieved 2010-08-30.
- ^ Thorsten Leemhuis (2011-09-13). "Kernel Log: x32 ABI gets around 64-bit drawbacks". www.h-online.com. Archived from the original on 28 October 2011. Retrieved 2011-11-01.
- ^ Giguere, Eric (1987-12-18). "The ANSI Standard: A Summary for the C Programmer". Retrieved 2010-09-04.
- ^ a b Meyers, Randy (2000-12-01). "The New C: Integers in C99, Part 1". drdobbs.com. Retrieved 2010-09-04.
- ^ a b c d "ISO/IEC 9899:201x" (PDF). open-std.org. section 6.2.6.2, paragraph 2. Retrieved 2016-06-20.
- ^ a b c "ISO/IEC 9899:201x" (PDF). open-std.org. section 5.2.4.2.1. Retrieved 2016-06-20.
- ^ "Fundamental types in C++". cppreference.com. Retrieved 5 December 2010.
- ^ "Chapter 8.6.2 on page 12" (PDF). ecma-international.org.
- ^ VB 6.0 help file
- ^ "The Integer, Long, and Byte Data Types (VBA)". microsoft.com. Retrieved 2006-12-19.
- ^ Giguere, Eric (December 18, 1987). "The ANSI Standard: A Summary for the C Programmer". Retrieved 2010-09-04.
- ^ "American National Standard Programming Language C specifies the syntax and semantics of programs written in the C programming language". Archived from the original on 2010-08-22. Retrieved 2010-09-04.
- ^ ECMAScript 6th Edition draft: https://people.mozilla.org/~jorendorff/es6-draft.html#sec-literals-numeric-literals Archived 2013-12-16 at the Wayback Machine