2 ms·
I think it's best to learn some basic maths by heart and also learn the idea so that you can reconstruct the rest if you forget or begin to doubt. The idea of
by tuukkah 3y ago
I think it's best to learn some basic maths by heart and also learn the idea so that you can reconstruct the rest if you forget or begin to doubt.
The idea of two's complement to remember: addition and subtraction for signed ints works the same way on the bit level as for unsigned ints. For signed ints, the highest bit is interpreted as the negative sign. That's it. (This is on top of the idea of how unsigned ints work ie. modulo arithmetic: a carry/borrow bit that doesn't fit in the int is thrown away)
The consequences to learn by heart if you work with signed and unsigned 8-bit ints (like in this article):
* The biggest unsigned int is 255 (2⁸-1; all 1s in binary) and smallest is 0 (all
0s in binary). Increment and decrement jump between those in overflow/underflow (the modulo arithmetic).
* As the highest bit is needed for the negative sign, the biggest signed int is 127 (2⁷-1; all 1s except the sign bit) and smallest is -128 (-2⁷; only the sign bit set). Increment and decrement jump between those in overflow/underflow.
I think the dial visualisations are great in this article for a mental model of the modulo arithmetic and how the overflow/underflow point is the difference between unsigned and signed arithmetic. Perhaps it would be even clearer to rotate them half a step so that the overflow point is straight up/down and the numbers with the highest bit set are all on the left side (hence 0 taking one of the slots on the positive side).