Integer (computer science) explained

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.

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, 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 . 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 through . 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 .

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

BitsNameRange (assuming two's complement for signed)Decimal digitsUsesImplementations
C/C++C#Pascal and DelphiJavaSQLFORTRANDRust
4nibble, semioctetSigned: From −8 to 7, from −(23) to 23 − 1Binary-coded decimal, single decimal digit rowspan="2"
Unsigned: From 0 to 15, which equals 24 − 1
8byte, octet, i8, u8Signed: From −128 to 127, from −(27) to 27 − 1ASCII characters, code units in the UTF-8 character encoding,
Unsigned: From 0 to 255, which equals 28 − 1,
16halfword, word, short, i16, u16Signed: From −32,768 to 32,767, from −(215) to 215 − 1UCS-2 characters, code units in the UTF-16 character encoding,,
Unsigned: From 0 to 65,535, which equals 216 − 1,
32word, long, doubleword, longword, int, i32, u32Signed: From −2,147,483,648 to 2,147,483,647, from −(231) to 231 − 1UTF-32 characters, true color with alpha, FourCC, pointers in 32-bit computing,,
Unsigned: From 0 to 4,294,967,295, which equals 232 − 1,,
;
64word, doubleword, longword, long, long long, quad, quadword, qword, int64, i64, u64Signed: From to Time (since the Unix epoch), pointers in 64-bit computing,,
Unsigned: From 0 to ,
128octaword, double quadword, i128, u128Signed: From to Complex scientific,IPv6 addresses,GUIDsC: only available as non-standard compiler-specific extension
Unsigned: From 0 to
nn-bit integer
(general case)
Signed: −(2n−1) to (2n−1 − 1)(n − 1) log10 2C23:, Ada
Unsigned: 0 to (2n − 1)n log10 2C23: Ada:, ; 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 class or Perl's "" package.[4] 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

See main article: article, Byte and Octet (computing). 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

See main article: article and Word (computer architecture). 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.

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

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.[5]

Standard integer

The standard integer size is platform-dependent.

In C, it is denoted by and required to be at least 16 bits. Windows and Unix systems have 32-bit s 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 . It is required to be at least 16 bits, and is often smaller than a standard integer, but this is not required. A conforming program can assume that it can safely store values between −(215−1)[6] and 215−1,[7] but it may not assume that the range is not larger. In Java, a is always a 16-bit integer. In the Windows API, the datatype is defined as a 16-bit signed integer on all machines.

Common short integer sizes
Programming languageData type nameSignednessSize in bytesMinimum valueMaximum value
C and C++signed2−32,767+32,767
unsigned2065,535
C#signed2−32,768+32,767
unsigned2065,535
Javasigned2−32,768+32,767
SQLsigned2 <-- (implementation-defined) -->−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 . 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) and 231−1, but it may not assume that the range is not larger.

Common long integer sizes
Programming languageApproval TypePlatformsData type nameStorage in bytesSigned rangeUnsigned range
C ISO/ANSI C99International StandardUnix, 16/32-bit systems
Windows, 16/32/64-bit systems
4
(minimum requirement 4)
−2,147,483,647 to +2,147,483,6470 to 4,294,967,295
(minimum requirement)
C ISO/ANSI C99International StandardUnix,
64-bit systems
8
(minimum requirement 4)
to 0 to
C++ ISO/ANSIInternational StandardUnix, Windows,
16/32-bit system
4 [8]
(minimum requirement 4)
−2,147,483,648 to +2,147,483,647
0 to 4,294,967,295
(minimum requirement)
C++/CLIInternational Standard
ECMA-372
Unix, Windows,
16/32-bit systems
4 [9]
(minimum requirement 4)
−2,147,483,648 to +2,147,483,647
0 to 4,294,967,295
(minimum requirement)
VBCompany StandardWindows4 [10] −2,147,483,648 to +2,147,483,647
VBACompany StandardWindows, Mac OS X4 [11] −2,147,483,648 to +2,147,483,647
SQL ServerCompany StandardWindows8 to 0 to
C#/ VB.NETECMA International StandardMicrosoft .NET or 8 to 0 to
JavaInternational/Company StandardJava platform8 to
Pascal?Windows, UNIX8 to 0 to (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 type did not exist in C++03. For an ANSI/ISO compliant compiler, the minimum requirements for the specified ranges, that is, −(263-1) to 263−1 for signed and 0 to 264−1 for unsigned, must be fulfilled; however, extending this range is permitted.[12] [13] 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.

Syntax

See main article: Integer literal.

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:

There are several alternate methods for writing integer literals in many programming languages:

See also

Notes and References

  1. Web site: Representation of numbers . Cheever . Eric . Swarthmore College . 2011-09-11.
  2. Web site: Madhusudhan Konda . A look at Java 7's new features - O'Reilly Radar . Radar.oreilly.com . 2011-09-02 . 2013-10-15.
  3. Book: Barr, Adam . The Problem with Software: Why Smart Engineers Write Bad Code . 2018-10-23 . MIT Press . 978-0-262-34821-8 . en.
  4. Web site: BigInteger (Java Platform SE 6) . Oracle . 2011-09-11 .
  5. News: Thorsten Leemhuis . 2011-09-13 . Kernel Log: x32 ABI gets around 64-bit drawbacks . https://web.archive.org/web/20111028081253/http://www.h-online.com/open/features/Kernel-Log-x32-ABI-gets-around-64-bit-drawbacks-1342061.html . 28 October 2011 . 2011-11-01 . www.h-online.com.
  6. Web site: ISO/IEC 9899:201x . section 6.2.6.2, paragraph 2. 2016-06-20 . open-std.org.
  7. Web site: ISO/IEC 9899:201x . section 5.2.4.2.1. 2016-06-20 . open-std.org.
  8. Web site: Fundamental types in C++. cppreference.com. 5 December 2010.
  9. Web site: Chapter 8.6.2 on page 12. ecma-international.org.
  10. VB 6.0 help file
  11. Web site: The Integer, Long, and Byte Data Types (VBA) . 2006-12-19 . microsoft.com.
  12. Web site: The ANSI Standard: A Summary for the C Programmer. Eric. Giguere. December 18, 1987. 2010-09-04.
  13. Web site: American National Standard Programming Language C specifies the syntax and semantics of programs written in the C programming language.. 2010-09-04. dead. https://web.archive.org/web/20100822072551/http://flash-gordon.me.uk/ansi.c.txt. 2010-08-22.
  14. ECMAScript 6th Edition draft: https://people.mozilla.org/~jorendorff/es6-draft.html#sec-literals-numeric-literals