Binary arithmetic and overflow
Addition and subtraction on n bits, subtraction through two's complement, shifts, carry and overflow flags, and what an overflow does in a real program.
- 23 cards
- 6 topics
- Free
The outline, topic by topic
Each topic is weighted the way the exam weighs it.
- Concepts
- Questions
- Sort and link
- The numbers
- Scenarios
- Understand
A few cards from the deck
Binary addition: what is it?
Show the answer
Addition done bit by bit from right to left, where 1 + 1 is 0 with a carry of 1, and 1 + 1 + 1 is 1 with a carry of 1.
0110 1101 + 0011 0111 = 1010 0100, that is 109 + 55 = 164. The circuit is the same for signed and unsigned integers: only how the result is interpreted changes.
What does this C program print?
```c
#include <stdio.h>
int main(void) {
unsigned char c = 250;
c += 10;
printf("%d\n", c);
return 0;
}`4260255-252
Show the answer
4cfits in 8 bits: 260 is truncated to 260 − 256 = 4. There is no error and no saturation at 255, and an unsigned type has no negative value.The … flag signals an overflow for unsigned integers, the … flag an overflow for signed integers.
Show the answer
The C flag signals an overflow for unsigned integers, the V flag an overflow for signed integers.
The arithmetic unit computes once and sets both flags; it is up to the program to look at the one matching the type it uses.
Integer overflow: what is it?
Show the answer
The situation where a calculation's result does not fit in the type's number of bits: the processor keeps only its low bits.
On 8 unsigned bits, 250 + 10 = 260 becomes 4 (260 − 256). In C, this is defined for unsigned types; for signed ones, the behaviour is undefined.