Integer
this wiki
An integer is a real number without fractional parts. The set of integers is .
In computer science, integers are sometimes divided into two categories (data types): signed, which can store both negative and nonnegative integers, and unsigned, which can only store nonnegative integers.
Representation of integers in computer memoryEdit
The most common representation of an integer is a 32bit (4byte) integer. Other common types are short (16bit, 2byte), long (64bit, 8byte) and byte (8bit, 1byte). Some programming frameworks and languages introduce arbitraryprecision integers, which can store any number of digits.
For unsigned integers, the bits are stored as if they were in binary in real life, considering the space for the data type. For example, for an unsigned byte:
Integer  Unsigned byte representation 

0  0000 0000 
1  0000 0001 
2  0000 0010 
3  0000 0011 
...  
255  1111 1111 
The range for 8 bits is 0 to 255, in general, the range is 0 to 2^{n}1 for n bits. Negative integers and values greater than 255 cannot be specified this way. To incorporate the sign with the representation, the binary system is extended to include it.
Binary  Signed signandmagnitude 

0 000 0000  0 
0 000 0001  1 
...  
0 111 1111  127 
1 000 0000  0 (negative zero) 
1 000 0000  1 
1 111 1111  127 
 Signandmagnitude
Here, the first bit represents the sign. 0 is positive and 1 is negative. As a consequence, a negative zero (1 000 0000) exists. (Negative zero is a consequence in computer science that arises from using this kind of representation) The range of values for 8 bits is 127 (1111 1111) to 127 (0111 1111). In general, the range is (2^{n}/21) to 2^{n}/21 for n bits.
Binary  Signed ones' complement 

0000 0000  0 
0000 0001  1 
0111 1110  126 
0111 1111  127 
1000 0000  127 
1000 0001  126 
1111 1110  1 
1111 1111  0 
 Ones' complement
The ones' complement system can also be used to represent negative numbers. A negative integer is represented by doing a bitwise negation (complement, bitwise NOT) to its positive counterpart. For example, the bitwise negation of 0000 0010 (2) is 1111 1101, which corresponds to 2. Again, negative zero (1111 1111) exists. Addition in binary is also possible with an endaround carry. The range of values is 127 to 127 ((2^{n}/21) to 2^{n}/21, in general).
Consequently, negative integers start with a bit 1 and positive integers start with a bit 0.
Binary  Signed twos' complement 

0000 0000  0 
0000 0001  1 
0111 1111  127 
1000 0000  128 
1000 0001  127 
1111 1111  1 
 Two's complement
The two's complement eliminated the need for an endaround carry system. The negative of a number is represented by the ones' complement (bitwise negation) of the number added by one. For example, to get the negative of 5 (0000 0101), do a ones' complement: 1111 1010, then add 1: 1111 1011 (5). To get the negative of 5 (positive 5), do a ones' complement: 0000 0100, then add 1: 0000 0101 (5). Notice that we arrived at the same result. There is only one zero in this system. The range is from 128 to 127, in general, (2^{n1}) to 2^{n}/21. This is the system commonly used.
Binary  Excess127  +127=  Unsigned 

0000 0000  127  0  
0000 0001  126  1  
0111 1111  0  127  
1000 0000  1  128  
1111 1111  128  255 
 ExcessN
ExcessN uses a predefined number N as a biasing value. For example, to convert 9 (0000 1001) to Excess127, add 127 to the number: 9 + 127 = 122, which corresponds to 0111 1010, 9's Excess127 representation.
Binary  Base 2 

0000 0000  0 
0000 0001  1 
0000 0010  2 (2+0) 
0000 0011  1 (2+1) 
0000 0100  4 (40+0) 
0000 0101  5 (40+1) 
0000 0110  2 (42+0) 
...  
1111 1111  85 (128+6432+168+42+1) 
 Base 2
This system uses a "base" of 2 to represent signed integers. From the right, the first bit is 1, then 2, then 4, then 8, and so on. The range is not symmetric; if the number of bits is even, the magnitude of the largest negative number that can be represented is twice as large as the largest positive number that can be represented, and vice versa if the word has an odd number of bits.
 Binarycoded decimal
Although this system is not that efficient, some machines still use this method. This system encodes each digit of a decimal (base10) number into binary. Each digit is stored in 4 bits. For example, 109 in BCD is 0001 0000 1001.
To represent signs, another group of 4 bits is added to the end of a BCD: 1101 for negative signed, 1101 for positive signed, and 1111 for unsigned.
 Gray code
The reflected binary code, also known as Gray code, is a binary numeral system where two successive values differ in only one digit.
Formal grammarEdit
The grammar for unsigned integers is:
<unsigned integer> → <nonzero digit><unsigned integer with trailing zeros>
<unsigned integer with trailing zeros> → <digit>  <unsigned integer with trailing zeros>
<digit> → "0"  "1"  "2"  "3"  "4"  "5"  "6"  "7"  "8"  "9"
<nonzero digit> → "1"  "2"  "3"  "4"  "5"  "6"  "7"  "8"  "9"
The grammar for signed integers is:
<signed integer> → +<unsigned integer>  <unsigned integer> (may sometimes implicitly cover the option <unsigned integer>)
In general, the grammar for integers is:
<integer> → <unsigned integer>  <signed integer>
Converting to storageEdit
 Convert decimal (base10) representation into a string. Using ASCII, each digit 0 to 10 would correspond to code points 30 to 39 (hex), respectively.
 Directly store its binary representation.
