In computer science, an integer is a datum of integral data type, a data type that represents some range of mathematical integers. Integral data types may be of different sizes and may or may not be allowed to contain negative values. Integers are commonly represented in a computer as a group of binary digits (bits). The size of the grouping varies so the set of integer sizes available varies between different types of computers. Computer hardware nearly always provides a way to represent a processor register or memory address as an integer.
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 source code of a program as a sequence of digits optionally prefixed with + or −. Some programming languages allow other notations, such as hexadecimal (base 16) or octal (base 8). Some programming languages also permit digit group separators. [2]
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 bits, using the binary numeral system. The order of the memory bytes storing the bits varies; see endianness. The width, precision, or bitness [3] of an integral type is the number of bits in its representation. An integral type with n bits can encode 2n numbers; for example an unsigned type typically represents the non-negative values 0 through 2n − 1. Other encodings of integer values to bit patterns are sometimes used, for example binary-coded decimal or Gray code, or as printed character codes such as ASCII.
There are four well-known ways to represent signed numbers in a binary computing system. The most common is two's complement, which allows a signed integral type with n bits to represent numbers from −2(n−1) through 2(n−1) − 1. Two's complement arithmetic is convenient because there is a perfect one-to-one correspondence between representations and values (in particular, no separate +0 and −0), and because addition, subtraction and multiplication do not need to distinguish between signed and unsigned types. Other possibilities include offset binary, sign-magnitude, and ones' complement.
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).
Bits | Name | Range (assuming two's complement for signed) | Decimal digits | Uses | Implementations | |||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
C/C++ | C# | Pascal and 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 | ASCII characters, code units in the UTF-8 character encoding | int8_t, signed char [b] | sbyte | Shortint | byte | tinyint | INTEGER [c] | 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 | UCS-2 characters, code units in the UTF-16 character encoding | int16_t, short, [b] int [b] | short | Smallint | short | smallint | INTEGER [c] | short | i16 |
Unsigned: From 0 to 65,535, which equals 216 − 1 | 4.82 | uint16_t, unsigned, [b] unsigned int [b] | ushort | Word | char [d] | 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 | UTF-32 characters, true color with alpha, FourCC, pointers in 32-bit computing | int32_t, int, [b] long [b] | int | LongInt; Integer [e] | int | int | INTEGER [c] | 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 [e] | — | unsigned int | — | uint | u32 | |||
64 | word, doubleword, longword, long, long long, quad, quadword, qword, int64, i64, u64 | Signed: From −(263) to 263 − 1 | 18.96 | Time (e.g. milliseconds since the Unix epoch), pointers in 64-bit computing | int64_t, long, [b] long long [b] | long | Int64 | long | bigint | INTEGER [c] | long | i64 |
Unsigned: From 0 to 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 −(2127) to 2127 − 1 | 38.23 | Complex scientific calculations, | Only available as non-standard or compiler-specific extensions | cent [f] | i128 | |||||
Unsigned: From 0 to 2128 − 1 | 38.53 | ucent [f] | 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: range0..2**n-1 , mod2**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 Lisp, Smalltalk, REXX, Haskell, Python, and Raku, support arbitrary precision integers (also known as infinite precision integers or bignums ). Other languages that do not support this concept as a top-level construct may have libraries available to represent very large numbers using arrays of smaller variables, such as Java's BigInteger class or Perl's "bigint" package. [6] These use as much of the computer's memory as is necessary to store the numbers; however, a computer has only a finite amount of storage, so they, too, can only represent a finite subset of the mathematical integers. These schemes support very large numbers; for example one kilobyte of memory could be used to store numbers up to 2466 decimal digits long.
A Boolean type is a type that can represent only two values: 0 and 1, usually identified with false and true respectively. This type can be stored in memory using a single bit, but is often given a full byte for convenience of addressing and speed of access.
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.
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.
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 embedded processors with 8- and 16-bit word size are still common. The 36-bit word length was common in the early days of computers.
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 stdint.h in the form of 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]
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.
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 [g] | +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 |
A long integer can represent a whole integer whose range is greater than or equal to that of a standard integer on the same machine.
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 | Unix, 16/32-bit systems [7] 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 | Unix, 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 | Unix, 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 | Windows, 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 |
C#/ 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 | ? | Windows, 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) |
In the C99 version of the C programming language and the C++11 version of C++, a long long
type is supported that has double the minimum capacity of the standard long
. This type is not supported by compilers that require C code to be compliant with the previous C++ standard, C++03, because the long long type did not exist in C++03. For an ANSI/ISO compliant compiler, the minimum requirements for the specified ranges, that is, −(263−1) [11] to 263−1 for signed and 0 to 264−1 for unsigned, [12] must be fulfilled; however, extending this range is permitted. [17] [18] This can be an issue when exchanging code and data between platforms, or doing direct hardware access. Thus, there are several sets of headers providing platform independent exact width types. The C standard library provides stdint.h ; this was introduced in C99 and C++11.
Integer literals can be written as regular Arabic numerals, consisting of a sequence of digits and with negation indicated by a minus sign before the value. However, most programming languages disallow use of commas or spaces for digit grouping. Examples of integer literals are:
42
10000
-233000
There are several alternate methods for writing integer literals in many programming languages:
0X
or 0x
to represent a hexadecimal value, e.g. 0xDEADBEEF
. Other languages may use a different notation, e.g. some assembly languages append an H
or h
to the end of a hexadecimal value.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.0755
. This was primarily intended to be used with Unix modes; however, it has been criticized because normal integers may also lead with zero. [19] As such, Python, Ruby, Haskell, and OCaml prefix octal values with 0O
or 0o
, following the layout used by hexadecimal values.0B
or 0b
.In many programming languages, there exist predefined constants representing the least and the greatest values representable with a given integer type.
Names for these include
MAXINT
[20] java.lang.Integer.MAX_VALUE
, java.lang.Integer.MIN_VALUE
[21] INT_MAX
, etc.[ citation needed ]MaxInt
[ citation needed ]sys.maxint
[ citation needed ]maxint
[23] In computing and electronic systems, binary-coded decimal (BCD) is a class of binary encodings of decimal numbers where each digit is represented by a fixed number of bits, usually four or eight. Sometimes, special bit patterns are used for a sign or other indications.
Hexadecimal is a positional numeral system that represents numbers using a radix (base) of sixteen. Unlike the decimal system representing numbers using ten symbols, hexadecimal uses sixteen distinct symbols, most often the symbols "0"–"9" to represent values 0 to 9 and "A"–"F" to represent values from ten to fifteen.
Octal is a numeral system with eight as the base.
In computer programming, a string is traditionally a sequence of characters, either as a literal constant or as some kind of variable. The latter may allow its elements to be mutated and the length changed, or it may be fixed. A string is generally considered as a data type and is often implemented as an array data structure of bytes that stores a sequence of elements, typically characters, using some character encoding. String may also denote more general arrays or other sequence data types and structures.
A computer number format is the internal representation of numeric values in digital device hardware and software, such as in programmable computers and calculators. Numerical values are stored as groupings of bits, such as bytes and words. The encoding between numerical values and bit patterns is chosen for convenience of the operation of the computer; the encoding used by the computer's instruction set generally requires conversion for external use, such as for printing and display. Different types of processors may have different internal representations of numerical values and different conventions are used for integer and real numbers. Most calculations are carried out with number formats that fit into a processor register, but some software systems allow representation of arbitrarily large numbers using multiple words of memory.
Double-precision floating-point format is a floating-point number format, usually occupying 64 bits in computer memory; it represents a wide range of numeric values by using a floating radix point.
In computer science and computer programming, a data type is a collection or grouping of data values, usually specified by a set of possible values, a set of allowed operations on these values, and/or a representation of these values as machine types. A data type specification in a program constrains the possible values that an expression, such as a variable or a function call, might take. On literal data, it tells the compiler or interpreter how the programmer intends to use the data. Most programming languages support basic data types of integer numbers, floating-point numbers, characters and Booleans.
In computer programming, a bitwise operation operates on a bit string, a bit array or a binary numeral at the level of its individual bits. It is a fast and simple action, basic to the higher-level arithmetic operations and directly supported by the processor. Most bitwise operations are presented as two-operand instructions where the result replaces one of the input operands.
In computer science, primitive data types are a set of basic data types from which all other data types are constructed. Specifically it often refers to the limited set of data representations in use by a particular processor, which all compiled programs must use. Most processors support a similar set of primitive data types, although the specific representations vary. More generally, primitive data types may refer to the standard data types built into a programming language. Data types which are not primitive are referred to as derived or composite.
A power of two is a number of the form 2n where n is an integer, that is, the result of exponentiation with number two as the base and integer n as the exponent.
The syntax of the C programming language is the set of rules governing writing of software in C. It is designed to allow for programs that are extremely terse, have a close relationship with the resulting object code, and yet provide relatively high-level data abstraction. C was the first widely successful high-level language for portable operating-system development.
Base32 is an encoding method based on the base-32 numeral system. It uses an alphabet of 32 digits, each of which represents a different combination of 5 bits (25). Since base32 is not very widely adopted, the question of notation—which characters to use to represent the 32 digits—is not as settled as in the case of more well-known numeral systems (such as hexadecimal), though RFCs and unofficial and de-facto standards exist. One way to represent Base32 numbers in human-readable form is using digits 0–9 followed by the twenty-two upper-case letters A–V. However, many other variations are used in different contexts. Historically, Baudot code could be considered a modified (stateful) base32 code. Base32 is often used to represent byte strings.
In computing, a word is the natural unit of data used by a particular processor design. A word is a fixed-sized datum handled as a unit by the instruction set or the hardware of the processor. The number of bits or digits in a word is an important characteristic of any specific processor design or computer architecture.
In computer programming, an integer overflow occurs when an arithmetic operation on integers attempts to create a numeric value that is outside of the range that can be represented with a given number of digits – either higher than the maximum or lower than the minimum representable value.
In the C programming language, data types constitute the semantics and characteristics of storage of data elements. They are expressed in the language syntax in form of declarations for memory locations or variables. Data types also determine the types of operations or methods of processing of data elements.
A bit field is a data structure that maps to one or more adjacent bits which have been allocated for specific purposes, so that any single bit or group of bits within the structure can be set or inspected. A bit field is most commonly used to represent integral types of known, fixed bit-width, such as single-bit Booleans.
This article compares a large number of programming languages by tabulating their data types, their expression, statement, and declaration syntax, and some common operating-system interfaces.
In computing, decimal32 is a decimal floating-point computer numbering format that occupies 4 bytes (32 bits) in computer memory.
In the C programming language, an escape sequence is specially delimited text in a character or string literal that represents one or more other characters to the compiler. It allows a programmer to specify characters that are otherwise difficult or impossible to specify in a literal.
In computer science, an integer literal is a kind of literal for an integer whose value is directly represented in source code. For example, in the assignment statement x = 1
, the string 1
is an integer literal indicating the value 1, while in the statement x = 0x10
the string 0x10
is an integer literal indicating the value 16, which is represented by 10
in hexadecimal.