All Topics  
Integer (computer science)

 

   Email Print
   Bookmark   Link






 

Integer (computer science)



 
 
In computer science, the term integer is used to refer to a data type
Data type

A data type in programming languages is an attribute of a data which tells the computer something about the kind of data it is. This involves setting constraints on the datum, such as what values it can take and what operations may be performed upon it....
 which represents some finite subset of the mathematical integer
Integer

The integers are natural numbers including 0 and their negative and non-negative numberss . They are numbers that can be written without a fractional or decimal component, and fall within the set ....
s. These are also known as integral data types.

Value and representation
The value of a datum
DATA

Debt, AIDS, Trade in Africa is a multinational Non-governmental organization founded in January 2002 in London by U2's Bono along with Robert Sargent Shriver III and activists from the Jubilee 2000 Drop the Debt campaign....
 with an integral type is the mathematical integer that it corresponds to. The representation of this datum is the way the value is stored in the computer’s memory.






Discussion
Ask a question about 'Integer (computer science)'
Start a new discussion about 'Integer (computer science)'
Answer questions from other users
Full Discussion Forum



Encyclopedia


In computer science, the term integer is used to refer to a data type
Data type

A data type in programming languages is an attribute of a data which tells the computer something about the kind of data it is. This involves setting constraints on the datum, such as what values it can take and what operations may be performed upon it....
 which represents some finite subset of the mathematical integer
Integer

The integers are natural numbers including 0 and their negative and non-negative numberss . They are numbers that can be written without a fractional or decimal component, and fall within the set ....
s. These are also known as integral data types.

Value and representation


The value of a datum
DATA

Debt, AIDS, Trade in Africa is a multinational Non-governmental organization founded in January 2002 in London by U2's Bono along with Robert Sargent Shriver III and activists from the Jubilee 2000 Drop the Debt campaign....
 with an integral type is the mathematical integer that it corresponds to. The representation of this datum is the way the value is stored in the computer’s memory. Integral types may be unsigned (capable of representing only non-negative integers) or signed (capable of representing negative integers as well).

The most common representation of a positive integer is a string of bit
Bit

A bit is a binary numeral system numerical digit, taking a value of either 0 or 1. Binary digits are a basic unit of information Computer data storage and transmission in digital computing and digital information theory....
s, using the binary numeral system
Binary numeral system

The binary numeral system, or notation with a radix of 2. Owing to its straightforward implementation in digital electronic circuitry using logic gates, the binary system is used internally by all modern computers....
. The order of the bits varies; see endianness
Endianness

In computing, endianness is the byte ordering used to represent some kind of data. Typical cases are the order in which integer values are stored as bytes in computer memory and the transmission order over a network or other medium....
. 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 three different ways to represent negative numbers in a binary numeral system. The most common is two’s complement
Two's complement

The two's complement of a binary number is defined as the value obtained by subtracting the number from a large power of two .A two's-complement system or two's-complement arithmetic is a system in which negative numbers are represented by the two's complement of the absolute value; this system is the most common Signed number r...
, 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
Bijection

In mathematics, a bijection, or a bijective function is a function f from a set X to a set Y with the property that, for every y in Y, there is exactly one x in X such that f = y....
 between representations and values, and because addition
Addition

Addition is the mathematics process of putting things together. The plus sign "+" means that numbers are added together. For example, in the picture on the right, there are 3 + 2 apples?meaning three apples and two other apples?which is the same as five apples, since 3 + 2 = 5....
, subtraction
Subtraction

Subtraction is one of the four basic arithmetic operations; it is the inverse of addition, meaning that if we start with any number and add any number and then subtract the same number we added, we return to the number we started with....
 and multiplication
Multiplication

Multiplication is the Operation of scaling one number by another. It is one of the four basic operations in elementary arithmetic .Multiplication is defined for Natural number in terms of repeated addition; for example, 4 multiplied by 3 can be calculated by adding 3 copies of 4 together:...
 do not need to distinguish between signed and unsigned types. The other possibilities are sign-magnitude and ones' complement. See Signed number representations
Signed number representations

In mathematics, negative numbers in any base are represented in the usual way, by prefixing them with a "−" sign. However, on a computer, there are various ways of representing a number's sign....
 for details.

Another, rather different, representation for integers is binary-coded decimal
Binary-coded decimal

In computing and electronics systems, binary-coded decimal is an encoding for decimal numbers in which each digit is represented by its own binary sequence....
, which is still commonly used in mainframe
Mainframe computer

Mainframes are computers used mainly by large organizations for critical applications, typically bulk data processing such as census, industry and consumer statistics, Enterprise Resource Planning, and financial transaction processing....
 financial applications and in databases.

Common integral data types


Bits Name Range Decimal digits Uses
8 byte
Byte

A byte is a basic unit of measurement of Computer storage in computer science. In many computer architectures it is a Byte addressing memory address space....
, octet
Octet (computing)

In computing, an octet is a grouping of eight bits.Octet, with the only exception noted below, always refers to an entity having exactly eight bits....
Signed: −128 to +127
127 (number)

127 is the natural number following 126 and preceding 128 ....
3 ASCII
ASCII

American Standard Code for Information Interchange , is a coding standard that can be used for interchanging information, if the information is expressed mainly by the written form of English words....
 characters, C
C (programming language)

C is a general-purpose computer programming language originally developed in 1972 by Dennis Ritchie at the Bell Telephone Laboratories to implement the Unix operating system....
/C++ char, C/C++ int8_t, Java byte, C# byte
Unsigned: 0 to +2553
16 halfword, word Signed: −32,768 to +32,767 5 UCS-2 characters, C/C++ short, C/C++ int16_t, Java short, C# short, Java char
Unsigned: 0 to +65,5355
32 word, long, doubleword, longword Signed: −2,147,483,648 to +2,147,483,647 10 UCS-4 characters, Truecolor
Truecolor

Truecolor is a method of representing and storing graphical image information in an RGB color space such that a very large number of colors, shades, and hues can be displayed in an image, such as in high quality photographic images or complex graphics....
 with alpha, C/C++ long (on Windows and 32-bit Unix), C/C++ int32_t, Java int, C# int, FourCC
FourCC

A FourCC is a sequence of four bytes used to uniquely identify data formats. The concept originated in the OSType scheme used in the Mac OS and was adopted for the Amiga/Electronic Arts Interchange File Format and derivatives....
Unsigned: 0 to +4,294,967,29510
64 doubleword, longword, long long, quad, quadword Signed: −9,223,372,036,854,775,808 to +9,223,372,036,854,775,807 19 C/C++ long (on 64-bit Unix), C/C++ long long, C/C++ int64_t, Java long, C# long
Unsigned: 0 to +18,446,744,073,709,551,61520
128 octaword Signed: −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 39 C only available as non-standard compiler-specific extension
Unsigned: 0 to +340,282,366,920,938,463,463,374,607,431,768,211,45539
n n-bit integer
(general case)
Signed: to Ada
Ada (programming language)

Ada is a structured programming, statically typed, Imperative programming, and Object-oriented programming high-level language computer programming programming language, extended from Pascal and other languages....
 range 2**(n-1) .. 2**n;
Unsigned: 0 to   Ada mod 2**n;
Different CPUs
Central processing unit

A central processing unit is an electronic circuit that can execute computer programs. This broad definition can easily be applied to many early computers that existed long before the term "CPU" ever came into widespread usage....
 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 (which can represent only the integers in a specified range).

Some languages, such as Lisp
Lisp programming language

Lisp is a family of computer programming languages with a long history and a distinctive, fully parenthesized syntax. Originally specified in 1958, Lisp is the second-oldest high-level programming language in widespread use today; only Fortran is older....
, REXX
REXX

REXX is an Interpreted language programming language which was developed at IBM. It is a structured high-level programming language which was designed to be both easy to learn and easy to read....
 and Haskell
Haskell (programming language)

Haskell is a standardized, purely functional programming language with non-strict programming language, named after logician Haskell Curry. The goals of the language are described as:...
, support arbitrary precision integers (also known as infinite precision integers or bignums). Other languages which 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
Perl

In computer programming, Perl is a high-level programming language, List of programming languages by category, Interpreter , dynamic programming language....
's "bigint" package. 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 digits long.

A Boolean
Boolean datatype

In computer science, the Boolean algebra datatype, sometimes called the logical datatype, is a primitive datatype having one of two values: Truth value and false....
 or Flag
Flag (computing)

In computer programming, flag refers to one or more bits that are used to store a binary numeral system value or code that has an assigned meaning....
 type is a type which 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
Nibble

A nibble is the computing term for a four-bit aggregation, or half an octet . As a nibble contains 4 bits, there are sixteen possible values, so a nibble corresponds to a single hexadecimal digit ....
 (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
Hexadecimal

In mathematics and computer science, hexadecimal is a numeral system with a radix, or base, of 16. It uses sixteen distinct symbols, most often the symbols 09 to represent values zero to nine, and A, B, C, D, E, F to represent values ten to fifteen....
 and holds one digit or a sign code in binary-coded decimal.

Data type names


Bits Signed Object Pascal Java C# VB.Net SQL92 VBScript C99 D
8 Yes ShortInt byte sbyte SByte   int8_t, signed char byte
16 Yes SmallInt short short, Int16 Short, Int16 smallint, int2 Integer int16_t, short short
32 Yes LongInt int int, Int32 Integer, Int32 integer, int, int4 Long int32_t, long, int int
64 Yes Int64 long long, Int64 Long, Int64 bigint, int8  int64_t, long long long
8 No Byte  byte
Byte

A byte is a basic unit of measurement of Computer storage in computer science. In many computer architectures it is a Byte addressing memory address space....
Byte
Byte

A byte is a basic unit of measurement of Computer storage in computer science. In many computer architectures it is a Byte addressing memory address space....
tinyint, int1 Byte
Byte

A byte is a basic unit of measurement of Computer storage in computer science. In many computer architectures it is a Byte addressing memory address space....
uint8_t, unsigned char ubyte
16 No Word char ushort, UInt16 UShort, UInt16   uint16_t, unsigned short ushort
32 No LongWord  uint, UInt32 UInteger, UInt32   uint32_t, unsigned long, unsigned int uint
64 No   ulong
Ulong

Ulong, New South Wales, is a small town in Australia.The town lies inland from Coffs Harbour by 36 km. Whilst a short distance, it takes 45 minutes to drive to, through magnificent rainforest....
, UInt64
ULong
Ulong

Ulong, New South Wales, is a small town in Australia.The town lies inland from Coffs Harbour by 36 km. Whilst a short distance, it takes 45 minutes to drive to, through magnificent rainforest....
, UInt64
   uint64_t, unsigned long long ulong


Note: C++
C++

C++ is a general-purpose programming language. It is regarded as a middle-level language, as it comprises a combination of both high-level programming language and low-level programming language language features....
 has no compiler-independent integer types with fixed bit widths. C has them only since C99
C99

C99 is a modern dialect of the C programming language....
, in the form (u)int(n)_t. It does specify the minimum widths for char, short, int, long, and long long (as shown in the table above). It also specifies that each of those types is no larger than the following, and that char is exactly one byte (eight bits in virtually all modern computers; the exact value is defined as CHAR_BIT in , also for older machines with wider bytes). Both C and C++ guarantee a char to be always 1 byte, but not necessarily 8 bits. C guarantees that char has at least 8 bits.

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 network
Computer network

A computer network is a group of interconnected computers. Networks may be classified according to a wide variety of characteristics. This article provides a general overview of some types and categories and also presents the basic components of a network....
ing, 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 which are handled simultaneously by processors of a particular architecture
Computer architecture

Computer architecture in computer engineering is the conceptual design and fundamental operational structure of a computer system. It is a blueprint and functional description of requirements and design implementations for the various parts of a computer, focusing largely on the way by which the central processing unit performs internally an...
. 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-, 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.

As of 2008 practically all new desktop processors are of the x86-64
X86-64

x86-64 is a superset of the x86. x86-64 Central processing units can run existing 32-bit or 16-bit x86 programs at full speed, but also support new programs written with a 64-bit address space and other additional capabilities....
 family and capable of using 64-bit words, they are however often used in 32-bit mode. Embedded processors
Embedded system

An embedded system is a special-purpose computer system designed to perform one or a few dedicated functions, often with real-time computing constraints....
 with 8- and 16-bit word size are still common. The 36-bit word length
36-bit word length

Many early computers aimed at the scientific market had a 36-bit word . This word length was just long enough to represent positive and negative integers to an accuracy of ten decimal digits ....
 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 , 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.

See also


  • Signed number representations
    Signed number representations

    In mathematics, negative numbers in any base are represented in the usual way, by prefixing them with a "−" sign. However, on a computer, there are various ways of representing a number's sign....
  • Integer
    Integer

    The integers are natural numbers including 0 and their negative and non-negative numberss . They are numbers that can be written without a fractional or decimal component, and fall within the set ....
     (the mathematical concept)