What is an integer?
An integer is a number without a decimal point -- for example: -312, 0, 18, 415, etc. Languages usually have both signed and unsigned integer types. Unsigned integers start at zero and go up: 0, 1, 2, 3, ... Signed integers include zero and both positive and negative numbers: ..., -3, -2, -1, 0, 1, 2, 3, ...
Sizes
How far up or down the numbers go depends on the size of the integer type. Sizes are measured in bytes. On modern machines, a byte is usually defined as 8 bits. Typical integer sizes include 1 byte, 2 bytes, 4 bytes and now 8 bytes.
Single Byte Values
1 byte gives 8 bits. For signed integers of this size the values range from -128 to 127 -- that is negative 128 to positive 127. For unsigned integers of this size the values range from 0 to 255. For each of these ranges, there are 256 distinct values. This is because 2 raised to the 8th power is 256.
Binary Values
Written in binary, a single byte integer goes from 00000000 to 11111111. Here is the sequence of the first 16 values:
- 00000000
- 00000001
- 00000010
- 00000011
- 00000100
- 00000101
- 00000110
- 00000111
- 00001000
- 00001001
- 00001010
- 00001011
- 00001100
- 00001101
- 00001110
- 00001111
If you continue this sequence, you will have 256 distinct values when you reach 11111111.
Here are the last 16 values of this sequence:
- 11110000
- 11110001
- 11110010
- 11110011
- 11110100
- 11110101
- 11110110
- 11110111
- 11111000
- 11111001
- 11111010
- 11111011
- 11111100
- 11111101
- 11111110
- 11111111
The 256 values corresponding to unsigned integers start with 00000000 as 0, 00000001 as 1, etc. until 11111111 as 255 is reached.
The 256 values corresponding to signed integers start with 00000000 as 0, 00000001 as 1, etc. until 01111111 as 127 is reached followed by 10000000 as -128, 10000001 as -127, etc. until 11111111 as -1 is reached.
Hexadecimal Values
As there are 8 places in a single byte value, this can be cumbersome to write. Hexadecimal is a way to represent the same values with a tighter notation using 16 symbols. Note that "hex" means "six" and "decimal" means "ten" so that "hex-a-decimal" means "six and ten" or "sixteen".
The sixteen symbols are 0 through 9 followed by A through F: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F
These correspond to the binary values like this:
- 0000 = 0
- 0001 = 1
- 0010 = 2
- 0011 = 3
- 0100 = 4
- 0101 = 5
- 0110 = 6
- 0111 = 7
- 1000 = 8
- 1001 = 9
- 1010 = A
- 1011 = B
- 1100 = C
- 1101 = D
- 1110 = E
- 1111 = F
Putting 2 of these symbols together allows us to represent a whole byte. Thus zero can be written as 00000000 in binary, 0 in decimal an 0x00 in hexadecimal.
The "0x" prefix is used to clarify to the compilers or interpreters that the value is hexadecimal and not decimal. For example, we write the decimal value 32 in hexadecimal as 0x20, but we write the hexadecimal value 0x32 as 50 in decimal. In some languages some other prefix or suffix might be used.
Double Byte Values
The 2 byte values use 16 bits. 2 to the 16th power (i.e., 2^16) gives 65536 distinct values. For unsigned integers, this ranges from 0 to 65535 (or 0x0000 to 0xFFFF in hexadecimal). For signed integers, this ranges from -32768 to 32767 -- that is negative 32768 to positive 32767.
Notice that if we were to write these values in binary, it would take a lot of room:
- 0 decimal = 0000 0000 0000 0000 binary = 0x0000 hexadecimal
- 65535 decimal = 1111 1111 1111 1111 binary = 0xFFFF hexadecimal
4 Byte Values
4 byte values use 32 bits giving 4,294,967,296 distinct values. Unsigned integers ranging from 0 to 4294967295. Signed integers ranging from -2147483648 to 2147482647.
8 Byte Values
8 byte values use 64 bits giving 18,446,744,073,709,551,616 distinct values with unsigned integers ranging from 0 to 18446744073709551615 and signed integers ranging from -9223372036854775808 to 9223372036854775807.
Integer Names
Depending on the language, there may or may not be a way to declare all of these integer sizes. Here are some examples.
C#
- sbyte - signed 8 bit
- byte - unsigned 8 bit
- short - signed 16 bit
- ushort - unsigned 16 bit
- int - signed 32 bit
- uint - unsigned 32 bit
- long - signed 64 bit
- ulong - unsigned 64 bit
Java
Note that Java does not use unsigned integer types.
- byte - signed 8 bit
- short - signed 16 bit
- int - signed 32 bit
- long - signed 64 bit
Other Languages
In other languages, such as C, the size can vary depending on the machine and the implementation of the compiler. Some of these languages use multiple words to for the data type. For example, "long" might be used for a signed type while "unsigned long" would be the name of the unsigned type of the same size.
Aliases
As C# is just one language of several that can be used to create programs for the .Net Framework, the names for the data types in C# are alias for .Net Framework data types. For example, the name byte is an alias for the type System.Byte and the name ulong is an alias for the type System.UInt64.
In C, macros are often used to create an alias for a type. For example, on a machine where the long data type is 32 bits, a macro might be defined for the unsigned long type called U32.
No comments:
Post a Comment