Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

Integer (computer science)

From Wikipedia, the free encyclopedia
(Redirected fromOctaword)
Datum of integral data type

In computer science, aninteger is adatum ofintegral data type, adata type that represents somerange of mathematicalintegers. 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 ofbinary 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 processorregister or memory address as an integer.

Value and representation

[edit]

Thevalue of an item with an integral type is the mathematical integer that it corresponds to. Integral types may beunsigned (capable of representing only non-negative integers) orsigned (capable of representing negative integers as well).[1]

An integer value is typically specified in thesource 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 permitdigit group separators.[2]

Theinternal 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 ofbits, using thebinary numeral system. The order of the memorybytes storing the bits varies; seeendianness. Thewidth,precision, orbitness[3] of an integral type is the number of bits in its representation. An integral type withn bits can encode 2n numbers; for example an unsigned type typically represents the non-negative values 0 through2n − 1. Other encodings of integer values to bit patterns are sometimes used, for examplebinary-coded decimal orGray code, or as printed character codes such asASCII.

There are four well-knownways to represent signed numbers in a binary computing system. The most common istwo's complement, which allows a signed integral type withn bits to represent numbers from−2(n−1) through2(n−1) − 1. Two's complement arithmetic is convenient because there is a perfectone-to-one correspondence between representations and values (in particular,no separate +0 and −0), and becauseaddition,subtraction andmultiplication do not need to distinguish between signed and unsigned types. Other possibilities includeoffset binary,sign-magnitude, andones' 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 oneprogramming language may be a different size in a different language, on a different processor, or in an execution context of different bitness; see§ Words.

Someolder computer architectures used decimal representations of integers, stored inbinary-coded decimal (BCD) or other format. These values generally require data sizes of 4 bits per decimal digit (sometimes called anibble), 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

[edit]
BitsNameRange (assumingtwo's complement forsigned)Decimal digitsUsesImplementations
C/C++C#Pascal andDelphiJavaSQL[a]FORTRANDRust
4nibble, semioctetSigned: From −8 to 7, from −(23) to 23 − 10.9Binary-coded decimal, single decimal digit repre­sen­ta­tion
Unsigned: From 0 to 15, which equals 24 − 11.2
8byte,octet, i8, u8Signed: From −128 to 127, from −(27) to 27 − 12.11ASCII characters,code units in theUTF-8character encodingint8_t,signed char[b]sbyteShortintbytetinyintINTEGER[c]bytei8
Unsigned: From 0 to 255, which equals 28 − 12.41uint8_t,unsigned char[b]byteByteunsigned tinyintubyteu8
16halfword,word, short, i16, u16Signed: From −32,768 to 32,767, from −(215) to 215 − 14.52UCS-2 characters,code units in theUTF-16character encodingint16_t,short,[b]int[b]shortSmallintshortsmallintINTEGER[c]shorti16
Unsigned: From 0 to 65,535, which equals 216 − 14.82uint16_t, unsigned,[b]unsigned int[b]ushortWordchar[d]unsigned smallintushortu16
32word,long, doubleword, longword, int, i32, u32Signed: From−2,147,483,648 to 2,147,483,647, from −(231) to 231 − 19.33UTF-32 characters,true color with alpha,FourCC, pointers in32-bit computingint32_t,int,[b]long[b]intLongInt;Integer[e]intintINTEGER[c]inti32
Unsigned: From 0 to 4,294,967,295, which equals 232 − 19.63uint32_t, unsigned,[b]unsigned int,[b]unsigned long[b]uintLongWord;DWord;Cardinal[e]unsigned intuintu32
64word, doubleword, longword, long, long long, quad, quadword, qword, int64, i64, u64Signed: From−(263) to263 − 118.96Time (e.g. milli­seconds since theUnix epoch), pointers in64-bit computingint64_t,long,[b]long long[b]longInt64longbigintINTEGER[c]longi64
Unsigned: From 0 to264 − 119.27uint64_t,unsigned long long[b]ulongUInt64;QWordunsigned bigintulongu64
128octaword, double quadword, i128, u128Signed: From−(2127) to2127 − 138.23Complex scientific cal­cula­tions,

IPv6 addresses,GUIDs

Only available as non-standard or compiler-specific extensionscent[f]i128
Unsigned: From 0 to2128 − 138.53ucent[f]u128
nn-bit integer
(general case)
Signed: −(2n−1) to (2n−1 − 1)(n − 1) log10 2C23:_BitInt(n),signed _BitInt(n)Ada:range-2**(n-1)..2**(n-1)-1
Unsigned: 0 to (2n − 1)n log10 2C23: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.

DifferentCPUs 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 havebit-field types (a specified number of bits, usually constrained to be less than the maximum hardware-supported width) andrange types (that can represent only the integers in a specified range).

Some languages, such asLisp,Smalltalk,REXX,Haskell,Python, andRaku, supportarbitrary precision integers (also known asinfinite precision integers orbignums). 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'sBigInteger class orPerl'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.

ABoolean type is a type that can represent only two values: 0 and 1, usually identified withfalse andtrue 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 anibble (when eating, being smaller than abite) ornybble (being a pun on the form of the wordbyte). One nibble corresponds to one digit inhexadecimal and holds one digit or a sign code in binary-coded decimal.

Bytes and octets

[edit]
Main articles:Byte andOctet (computing)

The termbyte 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 termbyte was usually not used at all in connection with bit- and word-addressed machines.

The termoctet always refers to an 8-bit quantity. It is mostly used in the field ofcomputer networking, where computers with different byte widths might have to communicate.

In modern usagebyte almost invariably means eight bits, since all other sizes have fallen into disuse; thusbyte has come to be synonymous withoctet.

Words

[edit]
Main article:Word (computer architecture)

The term 'word' is used for a small group of bits that are handled simultaneously by processors of a particulararchitecture. 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 aword is usually set by the first CPU in a family, rather than the characteristics of a later compatible CPU. The meanings of terms derived fromword, such aslongword,doubleword,quadword, andhalfword, also vary with the CPU and OS.[7]

Practically all new desktop processors are capable of using 64-bit words, thoughembedded processors with 8- and 16-bit word size are still common. The36-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 asint 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 aslong, 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 instdint.h in the form ofintptr_t.

Thebitness 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 ofMicrosoft Windows support existing 32-bit binaries, and programs compiled for Linux'sx32 ABI run in 64-bit mode yet use 32-bit memory addresses.[8]

Standard integer

[edit]

The standard integer size is platform-dependent.

InC, it is denoted byint and required to be at least 16 bits. Windows and Unix systems have 32-bitints on both 32-bit and 64-bit architectures.

Short integer

[edit]

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

InC, it is denoted byshort. 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. InJava, ashort isalways a 16-bit integer. In theWindows API, the datatypeSHORT is defined as a 16-bit signed integer on all machines.[7]

Common short integer sizes
Programming languageData type nameSignednessSize inbytesMinimum valueMaximum value
C andC++shortsigned2−32,767[g]+32,767
unsigned shortunsigned2065,535
C#shortsigned2−32,768+32,767
ushortunsigned2065,535
Javashortsigned2−32,768+32,767
SQLsmallintsigned2−32,768+32,767

Long integer

[edit]

Along integer can represent a wholeinteger whoserange is greater than or equal to that of a standard integer on the same machine.

InC, it is denoted bylong. 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.

Common long integer sizes
Programming languageApproval TypePlatformsData type nameStorage inbytesSigned rangeUnsigned range
C ISO/ANSI C99International StandardUnix, 16/32-bit systems[7]
Windows, 16/32/64-bit systems[7]
long4
(minimum require­ment 4)
−2,147,483,647 to +2,147,483,6470 to 4,294,967,295
(minimum require­ment)
C ISO/ANSI C99International StandardUnix,
64-bit systems[7][10]
long8
(minimum require­ment 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 StandardUnix,Windows,
16/32-bit system
long4[13]
(minimum require­ment 4)
−2,147,483,648 to +2,147,483,647
0 to 4,294,967,295
(minimum require­ment)
C++/CLIInternational Standard
ECMA-372
Unix,Windows,
16/32-bit systems
long4[14]
(minimum require­ment 4)
−2,147,483,648 to +2,147,483,647
0 to 4,294,967,295
(minimum require­ment)
VBCompany StandardWindowsLong4[15]−2,147,483,648 to +2,147,483,647
VBACompany StandardWindows,Mac OS XLong4[16]−2,147,483,648 to +2,147,483,647
SQL ServerCompany StandardWindowsBigInt8−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.NETECMA International StandardMicrosoft .NETlong orInt648−9,223,372,036,854,775,808 to +9,223,372,036,854,775,8070 to 18,446,744,073,709,551,615
JavaInternational/Company StandardJava platformlong8−9,223,372,036,854,775,808 to +9,223,372,036,854,775,807
Pascal?Windows,UNIXint648−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

[edit]
"long long" redirects here. For other uses, seeLong (disambiguation).

In theC99 version of theC programming language and theC++11 version ofC++, along long type is supported that has double the minimum capacity of the standardlong. This type is not supported by compilers that require C code to be compliant with the previous C++ standard, C++03, because thelong 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 Cstandard library providesstdint.h; this was introduced in C99 and C++11.

Syntax

[edit]
Main article:Integer literal

Integer literals can be written as regularArabic numerals, consisting of a sequence of digits and with negation indicated by aminus sign before the value. However, most programming languages disallow use of commas or spaces fordigit grouping. Examples of integer literals are:

  • 42
  • 10000
  • -233000

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

  • Many programming languages, especially those influenced byC, prefix an integer literal with0X or0x to represent ahexadecimal value, e.g.0xDEADBEEF. Other languages may use a different notation, e.g. someassembly languages append anH orh to the end of a hexadecimal value.
  • Perl,Ruby,Java,Julia,D,Go,C#,Rust andPython (starting from version 3.6) allow embeddedunderscores for clarity, e.g.10_000_000, and fixed-formFortran ignores embedded spaces in integer literals. C (starting fromC23) and C++ use single quotes for this purpose.
  • InC andC++, a leading zero indicates anoctal value, e.g.0755. This was primarily intended to be used withUnix modes; however, it has been criticized because normal integers may also lead with zero.[19] As such,Python,Ruby,Haskell, andOCaml prefix octal values with0O or0o, following the layout used by hexadecimal values.
  • Several languages, includingJava,C#,Scala,Python,Ruby,OCaml, C (starting from C23) and C++ can represent binary values by prefixing a number with0B or0b.

Extreme values

[edit]

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

See also

[edit]

Notes

[edit]
  1. ^Not all SQL dialects have unsigned datatypes.[4][5]
  2. ^abcdefghijklmn The sizes ofchar,short,int,long andlong long in C/C++ are dependent upon the implementation of the language.
  3. ^abcdFortan uses 'kinds' to control the size of integers. Parameterized constants defining the available kinds are available in the iso_fortran_env intrinsic module. Constants defining C compatible kinds are available in the iso_c_binding intrinsic module.
  4. ^ Java does not directly support arithmetic onchar types. The results must be cast back intochar from anint.
  5. ^ab The sizes of Delphi'sInteger andCardinal are not guaranteed, varying from platform to platform; usually defined asLongInt andLongWord respectively.
  6. ^ab Reserved for future use. Not implemented yet.
  7. ^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.[11]

References

[edit]
  1. ^Cheever, Eric."Representation of numbers". Swarthmore College. Retrieved2011-09-11.
  2. ^Madhusudhan Konda (2011-09-02)."A look at Java 7's new features - O'Reilly Radar". Radar.oreilly.com. Retrieved2013-10-15.
  3. ^Barr, Adam (2018-10-23).The Problem with Software: Why Smart Engineers Write Bad Code. MIT Press.ISBN 978-0-262-34821-8.
  4. ^"Sybase Adaptive Server Enterprise 15.5: Exact Numeric Datatypes".
  5. ^"MySQL 5.6 Numeric Datatypes".
  6. ^"BigInteger (Java Platform SE 6)". Oracle. Retrieved2011-09-11.
  7. ^abcdeFog, Agner (2010-02-16)."Calling conventions for different C++ compilers and operating systems: Chapter 3, Data Representation"(PDF). Retrieved2010-08-30.
  8. ^Thorsten Leemhuis (2011-09-13)."Kernel Log: x32 ABI gets around 64-bit drawbacks". www.h-online.com. Archived fromthe original on 28 October 2011. Retrieved2011-11-01.
  9. ^Giguere, Eric (1987-12-18)."The ANSI Standard: A Summary for the C Programmer". Retrieved2010-09-04.
  10. ^abMeyers, Randy (2000-12-01)."The New C: Integers in C99, Part 1". drdobbs.com. Retrieved2010-09-04.
  11. ^abcd"ISO/IEC 9899:201x"(PDF). open-std.org. section 6.2.6.2, paragraph 2. Retrieved2016-06-20.
  12. ^abc"ISO/IEC 9899:201x"(PDF). open-std.org. section 5.2.4.2.1. Retrieved2016-06-20.
  13. ^"Fundamental types in C++". cppreference.com. Retrieved5 December 2010.
  14. ^"Chapter 8.6.2 on page 12"(PDF). ecma-international.org.
  15. ^VB 6.0 help file
  16. ^"The Integer, Long, and Byte Data Types (VBA)". microsoft.com. Retrieved2006-12-19.
  17. ^Giguere, Eric (December 18, 1987)."The ANSI Standard: A Summary for the C Programmer". Retrieved2010-09-04.
  18. ^"American National Standard Programming Language C specifies the syntax and semantics of programs written in the C programming language". Archived fromthe original on 2010-08-22. Retrieved2010-09-04.
  19. ^ECMAScript 6th Edition draft:https://people.mozilla.org/~jorendorff/es6-draft.html#sec-literals-numeric-literalsArchived 2013-12-16 at theWayback Machine
  20. ^"SmallBASIC | MAXINT". Retrieved2025-01-20.
  21. ^"Integer (Java Platform SE 8 )". Retrieved2025-01-20.
  22. ^"Limits of Basic Types". Retrieved2025-01-20.
  23. ^Grogono, Peter (1995).Programming with Turing and Object Oriented Turing. New York: Springer. p. 363.doi:10.1007/978-1-4612-4238-3.LCCN 95010802.
Uninterpreted
Numeric
Pointer
Text
Composite
Other
Related
topics
Retrieved from "https://en.wikipedia.org/w/index.php?title=Integer_(computer_science)&oldid=1278095594#Words"
Categories:
Hidden categories:

[8]ページ先頭

©2009-2025 Movatter.jp