# Integer (computer science)

Last updated

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 provide a way to represent a processor register or memory address as an integer.

## 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 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 or precision 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 or on a different processor.

## Common integral data types

BitsNameRange (assuming two's complement for signed)Decimal digitsUsesImplementations
C/C++ C# Pascal and Delphi Java SQL [lower-alpha 1] FORTRAN D
4
nibble, semioctet Signed: From −8 to 7, from −(23) to 23  1
0.9
Binary-coded decimal, single decimal digit representationn/an/an/an/an/an/an/a
Unsigned: From 0 to 15, which equals 24  1
1.2
8
byte, octet, i8, u8Signed: 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 [lower-alpha 2] sbyteShortintbytetinyintinteger(1)byte
Unsigned: From 0 to 255, which equals 28  1
2.41
uint8_t, unsigned char [lower-alpha 2] byteByten/aunsigned tinyintn/aubyte
16
halfword, word, short, i16, u16Signed: 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 [lower-alpha 2] , int [lower-alpha 2] shortSmallintshortsmallintinteger(2)short
Unsigned: From 0 to 65,535, which equals 216  1
4.82
uint16_t, unsigned [lower-alpha 2] , unsigned int [lower-alpha 2] ushortWordchar [lower-alpha 3] unsigned smallintn/aushort
32
word, long, doubleword, longword, int, i32, u32Signed: 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 [lower-alpha 2] , long [lower-alpha 2] intLongInt; Integer [lower-alpha 4] intintinteger(4)int
Unsigned: From 0 to 4,294,967,295, which equals 232  1
9.63
uint32_t, unsigned [lower-alpha 2] , unsigned int [lower-alpha 2] , unsigned long [lower-alpha 2] uintLongWord; DWord; Cardinal [lower-alpha 4] n/aunsigned intn/auint
64
word, doubleword, longword, long long, quad, quadword, qword, int64, i64, u64Signed: 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 [lower-alpha 2] , long long [lower-alpha 2] longInt64longbigintinteger(8)long
Unsigned: From 0 to 18,446,744,073,709,551,615, which equals 264  1
19.27
uint64_t, unsigned long long [lower-alpha 2] ulongUInt64; QWordn/aunsigned bigintn/aulong
128
octaword, double quadword, i128, u128Signed: 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,

C: only available as non-standard compiler-specific extensionn/an/an/an/ainteger(16)cent [lower-alpha 5]
Unsigned: From 0 to 340,282,366,920,938,463,463,374,607,431,768,211,455, which equals 2128  1
38.53
n/aucent [lower-alpha 5]
n
n-bit integer
(general case)
Signed: −(2n−1) to (2n−1  1)(n  1) log10 2 Ada: range -2**(n-1)..2**(n-1)-1
Unsigned: 0 to (2n − 1)n log10 2Ada: 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 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. [5] 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 or Flag 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.

### 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. [6]

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`.

### 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. [7] [8] A conforming program can assume that it can safely store values between −(215−1) [9] and 215−1, [10] but it may not assume that the range isn't 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. [6]

Common short integer sizes
Programming language Data type name Signedness Size in bytes Minimum valueMaximum value
C and C++ shortsigned2−32,767 [lower-alpha 6] +32,767
unsigned shortunsigned2065,535
C# shortsigned2−32,768+32,767
ushortunsigned2065,535
Java shortsigned2−32,768+32,767

### Long integer

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) [9] and 231−1, [10] but it may not assume that the range isn't larger.

Common long integer sizes
Programming language Approval Type Platforms Data type nameStorage in bytes Signed range Unsigned range
C ISO/ANSI C99International Standard Unix,16/32-bit systems [6]
Windows,16/32/64-bit systems [6]
long [lower-alpha 7] 4
(minimum requirement 4)
−2,147,483,647 to +2,147,483,6470 to 4,294,967,295
(minimum requirement)
C ISO/ANSI C99International Standard Unix,
64-bit systems [6] [8]
long [lower-alpha 7] 8
(minimum requirement 4)
−9,223,372,036,854,775,807 to +9,223,372,036,854,775,8070 to 18,446,744,073,709,551,615
C++ ISO/ANSIInternational Standard Unix, Windows,
16/32-bit system
long [lower-alpha 7] 4 [12]
(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 [lower-alpha 7] 4 [13]
(minimum requirement 4)
−2,147,483,648 to +2,147,483,647
0 to 4,294,967,295
(minimum requirement)
VB Company Standard Windows Long4 [14] −2,147,483,648 to +2,147,483,647N/A
VBA Company Standard Windows, Mac OS X Long4 [15] −2,147,483,648 to +2,147,483,647N/A
SQL Server Company Standard Windows BigInt8−9,223,372,036,854,775,808 to +9,223,372,036,854,775,8070 to 18,446,744,073,709,551,615
C#/ VB.NET ECMA International Standard Microsoft .NET long or Int648−9,223,372,036,854,775,808 to +9,223,372,036,854,775,8070 to 18,446,744,073,709,551,615
Java International/Company Standard Java platform long8−9,223,372,036,854,775,808 to +9,223,372,036,854,775,807N/A
Pascal ? Windows, UNIX int648−9,223,372,036,854,775,808 to +9,223,372,036,854,775,8070 to 18,446,744,073,709,551,615 (Qword type)

### Long long

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, −(2631) [9] to 263−1 for signed and 0 to 264−1 for unsigned, [10] must be fulfilled; however, extending this range is permitted. [16] [17] 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.

## Notes

1. Not all SQL dialects have unsigned datatypes. [3] [4]
2. The sizes of char, short, int, long and long long in C/C++ are dependent upon the implementation of the language.
3. Java does not directly support arithmetic on char types. The results must be cast back into char from an int.
4. The sizes of Delphi's Integer and Cardinal are not guaranteed, varying from platform to platform; usually defined as LongInt and LongWord respectively.
5. Reserved for future use. Not implemented yet.
6. 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. [9]
7. The terms long and int are equivalent [11]

## Related Research Articles

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.

In computing, endianness is the order or sequence of bytes of a word of digital data in computer memory. Endianness is primarily expressed as big-endian (BE) or little-endian (LE). A big-endian system stores the most significant byte of a word at the smallest memory address and the least significant byte at the largest. A little-endian system, in contrast, stores the least-significant byte at the smallest address. Endianness may also be used to describe the order in which the bits are transmitted over a communication channel, e.g., big-endian in a communications channel transmits the most significant bits first. Bit-endianness is seldom used in other contexts.

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.

In computer science and computer programming, a data type or simply type is an attribute of data which 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. A data type constrains the values that an expression, such as a variable or a function, might take. This data type defines the operations that can be done on the data, the meaning of the data, and the way values of that type can be stored. A data type provides a set of values from which an expression may take its values.

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 type is either of the following:

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.

In computing, a memory address is a reference to a specific memory location used at various levels by software and hardware. Memory addresses are fixed-length sequences of digits conventionally displayed and manipulated as unsigned integers. Such numerical semantic bases itself upon features of CPU, as well upon use of the memory like an array endorsed by various programming languages.

In computing, signedness is a property of data types representing numbers in computer programs. A numeric variable is signed if it can represent both positive and negative numbers, and unsigned if it can only represent non-negative numbers.

In computing, a word is the natural unit of data used by a particular processor design. A word is a fixed-sized piece of data handled as a unit by the instruction set or the hardware of the processor. The number of bits in a word is an important characteristic of any specific processor design or computer architecture.

IEC 61131-3 is the third part of the open international standard IEC 61131 for programmable logic controllers, and was first published in December 1993 by the IEC. The current (third) edition was published in February 2013.

In computer architecture, 128-bit integers, memory addresses, or other data units are those that are 128 bits wide. Also, 128-bit CPU and ALU architectures are those that are based on registers, address buses, or data buses of that size.

In computer programming, an integer overflow occurs when an arithmetic operation 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 used in computer programming. It consists of a number of adjacent computer memory locations which have been allocated to hold a sequence of bits, stored so that any single bit or group of bits within the set can be addressed. A bit field is most commonly used to represent integral types of known, fixed bit-width.

A class in C++ is a user-defined type or data structure declared with keyword `class` that has data and functions as its members whose access is governed by the three access specifiers private, protected or public. By default access to members of a C++ class is private. The private members are not accessible outside the class; they can be accessed only through methods of the class. The public members form an interface to the class and are accessible outside the class.

sizeof is a unary operator in the programming languages C and C++. It generates the storage size of an expression or a data type, measured in the number of char-sized units. Consequently, the construct sizeof (char) is guaranteed to be 1. The actual number of bits of type char is specified by the preprocessor macro CHAR_BIT, defined in the standard include file limits.h. On most modern computing platforms this is eight bits. The result of sizeof has an unsigned integer type that is usually denoted by size_t.

In computing, bit numbering is the convention used to identify the bit positions in a binary number or a container of such a value. The bit number starts with zero and is incremented by one for each subsequent bit position.

A variable-length quantity (VLQ) is a universal code that uses an arbitrary number of binary octets to represent an arbitrarily large integer. A VLQ is essentially a base-128 representation of an unsigned integer with the addition of the eighth bit to mark continuation of bytes. VLQ is identical to LEB128 except in endianness. See the example below.

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.

## References

1. Cheever, Eric. "Representation of numbers". Swarthmore College. Retrieved 2011-09-11.
2. Madhusudhan Konda (2011-09-02). "A look at Java 7's new features - O'Reilly Radar". Radar.oreilly.com. Retrieved 2013-10-15.
3. "BigInteger (Java Platform SE 6)". Oracle. Retrieved 2011-09-11.
4. Fog, Agner (2010-02-16). "Calling conventions for different C++ compilers and operating systems: Chapter 3, Data Representation" (PDF). Retrieved 2010-08-30.
5. Giguere, Eric (1987-12-18). "The ANSI Standard: A Summary for the C Programmer" . Retrieved 2010-09-04.
6. Meyers, Randy (2000-12-01). "The New C: Integers in C99, Part 1". drdobbs.com. Retrieved 2010-09-04.
7. "ISO/IEC 9899:201x" (PDF). open-std.org. section 6.2.6.2, paragraph 2. Retrieved 2016-06-20.
8. "ISO/IEC 9899:201x" (PDF). open-std.org. section 5.2.4.2.1. Retrieved 2016-06-20.
9. "ISO/IEC 9899:201x" (PDF). open-std.org. Retrieved 2013-03-27.
10. "Fundamental types in C++". cppreference.com. Retrieved 5 December 2010.
11. "Chapter 8.6.2 on page 12" (PDF). ecma-international.org.
12. VB 6.0 help file
13. "The Integer, Long, and Byte Data Types (VBA)". microsoft.com. Retrieved 2006-12-19.
14. Giguere, Eric (December 18, 1987). "The ANSI Standard: A Summary for the C Programmer" . Retrieved 2010-09-04.
15. "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.