Integer
From bildr
A whole number. 1,2,3,4,5, -689 etc. 1.2 is not an integer, but a real number. In programing, there are usually represented as a float or a double.
Contents |
In programing
Integers are important in microprocessors because they generally operate on them faster than on any other type and they are the simplest numerical type to use. When programming, they are usually specified by how many bits you use to store them, and whether or not they are signed. While there are several ways of representing negative numbers, the overwhelmingly most common way is Two's Complement. In all representations, the sign bit is the MSB in the integer.
| Bit size | Signed Min | Signed Max | Unsigned Max |
|---|---|---|---|
| 8 | -128 | 127 | 255 |
| 16 | -32,768 | 32,767 | 65,535 |
| 32 | −2,147,483,648 | 2,147,483,647 | 4,294,967,295 |
| 64 | −9,223,372,036,854,775,808 | 9,223,372,036,854,775,807 | 18,446,744,073,709,551,615 |
Byte ordering
Integers are stored in memory based on the byte ordering that the microprocessor uses, which can be little endian or big endian. Little endian means that the LSB is stored at the lower memory address. Big endian reverses this. It's important to keep this in mind when interpreting memory byte dumps since they usually show the memory from left to right, which is not how you would expect to see it.
Mathematical Operations
Integer math has two main "gotchas".
Overflow
When you perform a math operation (like add or multiply) on integers and the result is number that is larger than what your integer can hold, you overflow. Basically, take your result and remove any bits that are beyond the bit-length of your integer:
binary decimal (8-bit unsigned)
11111111 255
+ 00000001 +1
............ ...
1 00000000 0
^^^^^^^^ <-- We only keep the lower 8 bits
Most small microprocessors will have a carry flag that indicates when this happens. However, if you do it with signed integers, overflow can happen within the confines of the bit length and may not trigger the carry flag. Check your inputs and outputs for sanity and save debugging time.
Rounding
Most microprocessors support division, but they do not round; they truncate.
binary decimal (8-bit unsigned)
00000101 5
÷ 00000010 2
............ ...
00000010 2
There will almost always be an operator to get the remainder.
This page is an Article on bildr. Articles are pages that define or explain a concept, method, or generic item.