# Arithmetic at Machine Level

Computer Science I · Introduction to Computers · https://tryals.app/learn/computer-science-i/arithmetic-at-machine-level

## Doing Sums in Binary

Computers add binary numbers using carry rules where $1+1=10$ (which is $0$ carry $1$). Adding $011 + 001$ yields $100$: the rightmost $1+1$ makes $0$ carry $1$, the middle column becomes $0$ carry $1$, and the final column takes the carry.

Because a register holds a **fixed** width, any sum requiring extra bits suffers from **overflow**: the high bit is lost. In an 8-bit register, $255 + 1$ wraps to $0$. This hardware behavior is entirely silent; software must account for it explicitly.

## Signed Numbers and Two's Complement

Negative values use **two's complement**, where you **invert all bits and add 1**. The leftmost bit is the **sign bit** ($0$ for positive, $1$ for negative). This lets the standard adder handle subtraction natively.

| 8-bit pattern | Value |
|---|---|
| $00000001$ | $+1$ |
| $11111111$ | $-1$ |
| $10000000$ | $-128$ |
| $01111111$ | $+127$ |

> **Pitfall:** Assuming integers grow infinitely. Machine limits mean signed $+127 + 1$ wraps silently to $-128$. Always watch your fixed widths!

## Practice questions

3 of this lesson's 10 practice questions, with answers. The full set is in the app.

### 1. An 8-bit signed integer holds −128 to +127, one more negative value than positive. Where does the asymmetry come from?

A. From the sign bit being reserved, which removes one positive value
B. From an arbitrary convention that could equally have gone the other way
C. From rounding, which favours negative values in binary
D. From two's complement having only one zero, leaving one spare negative

**Answer:** D. From two's complement having only one zero, leaving one spare negative

**Why:** 256 patterns, one zero, and the spare goes to the negatives. Sign-magnitude wastes a pattern on −0 and gets a symmetric range; two's complement spends it on −128 instead. The consequence is real: negating −128 overflows, which is a genuine edge case in production code.

Page: https://tryals.app/practice/computer-science-i/arithmetic-at-machine-level/an-8-bit-signed-integer-holds-128-to-127-one-more-negative-value

### 2. Each option is an addition performed in an 8-bit UNSIGNED register. Select every one that overflows.

A. 200 + 100
B. 255 + 1
C. 128 + 127
D. 240 + 16
E. 100 + 100

**Answer:** A. 200 + 100; B. 255 + 1; D. 240 + 16

**Why:** 200 + 100 = 300, 255 + 1 = 256 and 240 + 16 = 256 all exceed 255 and wrap. 128 + 127 = 255 fits exactly, and 100 + 100 = 200 fits easily. The tempting error is 128 + 127, which looks like it should overflow because both numbers are large, but the boundary is about the sum, not the operands.

Page: https://tryals.app/practice/computer-science-i/arithmetic-at-machine-level/each-option-is-an-addition-performed-in-an-8-bit-unsigned-register

### 3. Two's complement is used because it lets one adder circuit perform both addition and subtraction.

**Answer:** True

**Why:** True. Negating in two's complement is invert-and-add-one, after which $a - b$ is just $a + (-b)$ through the same adder, with the carry out of the top bit discarded. Sign-magnitude would need separate subtract logic and has two zeroes; two's complement has one and wraps cleanly.

Page: https://tryals.app/practice/computer-science-i/arithmetic-at-machine-level/twos-complement-is-used-because-it-lets-one-adder-circuit-perform
