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).
An integer value is typically specified in the (source code of the) program as a sequence of digits, without spaces or thousands separators, optionally prefixed with + or −. Some programming languages allow other notations, such as hexadecimal (base 16) or octal (base 8).
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. Typically all integers from the minimum through the maximum can be represented.
The maximum is sometimes called MAXINT
or—as in the C standard library limits.h header—INT_MAX
.
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.
There are four different ways to represent negative numbers in a binary numeral 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. The other possibilities are offset binary, sign-magnitude and ones' complement. See Signed number representations for details.
Read more about this topic: Integer (computer Science)