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|
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.
Integer math has two main "gotchas".
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.
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.