---
title: Binary, hex and two's complement
heading: Writing numbers the way hardware stores them
eyebrow: C7 · Lesson 2 · Binary, hex and two's complement
xp: 40
---

::: hook
A digital circuit stores numbers as rows of bits: wires and flip-flops that are each 0 or 1. Eight of them make a byte, which can mean 0 to 255, or −128 to +127, depending only on how you choose to read it. Engineers write bit patterns in hexadecimal, where each digit stands for four bits, so a whole byte fits in two characters. And negative numbers use **two's complement**, which lets the same adder circuit subtract.

::: play
- The dots are the eight bits of a byte, most significant on the left. Slide n to set its value.
- Watch the readouts as you cross 127 to 128. What happens to the value read as a signed number?
- Find a value whose hex is all letters. How many bits does each hex digit cover?
:::
:::

```sim plot
view: graph
x: bit = -0.5..7.5
y: value = -0.3..1.3
slider: n = 0..255 step 1 = 77
point: 0, floor(n/128) - 2*floor(n/256) | label: b7
point: 1, floor(n/64) - 2*floor(n/128) | label: b6
point: 2, floor(n/32) - 2*floor(n/64) | label: b5
point: 3, floor(n/16) - 2*floor(n/32) | label: b4
point: 4, floor(n/8) - 2*floor(n/16) | label: b3
point: 5, floor(n/4) - 2*floor(n/8) | label: b2
point: 6, floor(n/2) - 2*floor(n/4) | label: b1
point: 7, n - 2*floor(n/2) | label: b0
readout: binary = n | format: bin8
readout: hex = n | format: hex2
readout: unsigned = n | format: plain
readout: signed (two's complement) = n - 256*step(n - 127.5) | format: plain
```

::: predict
## In 8-bit two's complement, what pattern represents −1?

- [ ] 10000001
- [ ] 01111111
- [x] 11111111
- [ ] 10000000

::: reveal
11111111. Add 1 to it and it rolls over to 00000000 (the carry out of the top bit is dropped), which is exactly what −1 + 1 should give. In general, an 8-bit pattern with value n means $n - 256$ if its top bit is set. 10000000 is −128, the most negative; 01111111 is +127, the most positive.
:::
:::

::: nameit
## Number formats

**Binary.** Bit k is worth $2^k$: $1011_2 = 8 + 2 + 1 = 11$. An n-bit unsigned number spans 0 to $2^n - 1$.

**Hexadecimal.** Base 16, digits 0 to 9 then A to F (10 to 15). Each hex digit is exactly four bits, so converting is just grouping:

$$1101\,0110_2 = \text{D6}_{16} = 13 \times 16 + 6 = 214$$

It's written 0xD6 in C and Verilog's 8'hD6.

**Two's complement.** To represent $-m$ in n bits, store $2^n - m$. Equivalently, invert every bit of m and add 1. The top bit then acts as a sign bit, worth $-2^{n-1}$:

- range: $-2^{n-1}$ to $2^{n-1} - 1$ (−128 to 127 for a byte);
- the pattern for $-m$ plus the pattern for m gives $2^n$, which overflows to 0: addition just works;
- subtraction is adding the two's complement, so one adder circuit does both.

**Overflow.** If two numbers of the same sign add to a result of the other sign, the true answer didn't fit: 100 + 100 in signed 8 bits gives −56.

**Sign extension.** To widen a signed number, copy its top bit into the new bits: −3 is 1101 in 4 bits and 1111 1101 in 8.
:::


::: example
## What does the 8-bit pattern 0xB6 mean, unsigned and in two's complement?

::: solution
1. **Unsigned value.**
   $$\begin{aligned} \text{0xB6} &= 1011\,0110_2 \\ &= 128 + 32 + 16 + 4 + 2 \\ &= 182 \end{aligned}$$
2. **Two's-complement value.** Its top bit is 1, so subtract 256:
   $$182 - 256 = -74$$

**Answer:** 182 unsigned, and −74 in two's complement.
:::
:::

::: example
## Write −45 as an 8-bit two's-complement number, in binary and hex.

::: solution
1. **Write 45 in binary.**
   $$45 = 0010\,1101_2$$
2. **Invert every bit.**
   $$1101\,0010$$
3. **Add 1.**
   $$1101\,0011 = \text{0xD3}$$
4. **Check.** 0xD3 is 211 unsigned, and
   $$211 - 256 = -45$$

**Answer:** −45 is 1101 0011 in binary, 0xD3 in hex.
:::
:::

::: example
## Add 0x64 and 0x50 as 8-bit two's-complement numbers. What's the result, and has it overflowed?

::: solution
1. **Convert to decimal.** 0x64 = 100 and 0x50 = 80.
2. **Add.**
   $$\begin{aligned} 100 + 80 &= 180 \\ &= \text{0xB4} \end{aligned}$$
   which fits in 8 bits with no carry out.
3. **Read it as signed.** Its top bit is 1, so as a signed number it reads
   $$180 - 256 = -76$$
4. **Spot the overflow.** Two positive numbers gave a negative result: signed overflow, since +180 is beyond the 8-bit range of −128 to +127.
5. **The rule.** Overflow happens exactly when two operands of the same sign give a result of the other sign; the carry out of the top bit tells you only about unsigned overflow.

**Answer:** the result is 0xB4, which reads as −76 signed: the addition has overflowed.
:::
:::

::: check
given: n = 1..255 int
answer: n
base: 2
width: 8

## Write {n} as an 8-bit binary number.

::: right
Right: {answer}.
:::

::: misses
- [n + 1] Off by one: check the lowest bit.
- [any] Not yet. Subtract the largest power of 2 that fits, set that bit, and repeat.
:::
:::

::: check
given: n = 1..255 int
answer: n
tolerance: 0

## What is the binary number {n:bin8} in decimal?

::: right
Right: {answer}.
:::

::: misses
- [any] Not yet. Add $2^k$ for each 1, counting k from 0 at the right.
:::
:::

::: check
given: n = 16..255 int
answer: n
base: 16
width: 2

## Write {n} in hexadecimal.

::: right
Right: {answer}.
:::

::: misses
- [any] Not yet. Divide by 16: the quotient is the first digit and the remainder the second (10 to 15 are A to F).
:::
:::

::: check
given: m = 1..63 int
answer: 256 - m
base: 2
width: 8

## Write −{m} in 8-bit two's complement.

::: right
Right: {answer}, which is $256 - \given{m}$.
:::

::: misses
- [255 - m] That inverts the bits; now add 1.
- [128 + m] That's sign-and-magnitude, not two's complement.
- [any] Not yet. Invert the bits of {m} and add 1, or compute $256 - \given{m}$.
:::
:::

::: check
given: n = 128..255 int
answer: n - 256
tolerance: 0

## The 8-bit pattern {n:bin8} is read as a two's-complement signed number. What value is it?

::: right
Right: {answer}.
:::

::: misses
- [n] That's the unsigned reading. The top bit is set, so subtract 256.
- [any] Not yet. With the top bit set, the value is the unsigned value minus 256.
:::
:::

::: check
kind: parts
given: a = 100 | 110 | 120; b = 30 | 60 | 90

## Two positive 8-bit two's-complement numbers, {a} and {b}, are added.

::: part
answer: a + b - 256
tolerance: 0

### What signed value does the 8-bit result read as?

::: misses
- [a + b] Above +127 the result wraps around: subtract 256.
- [any] Keep the low 8 bits and read the top bit as −128.
:::
:::

::: part
answer: a + b
tolerance: 0

### What does the same 8-bit pattern read as unsigned?

::: misses
- [any] The plain sum, since it's below 256.
:::
:::
:::

::: check
kind: derivation
vars: n = 2..32

## Numbers are stored in n bits.

::: step
answer: 2^n - 1

### What's the largest unsigned value?
:::

::: step
answer: 2^(n - 1) - 1

### The largest two's-complement value?
:::

::: step
answer: -2^(n - 1)

### The most negative?
:::

::: right
Right. Two's complement gives one more negative value than positive.
:::
:::

::: explain
## Why does two's complement let the same adder circuit handle both positive and negative numbers?

::: model
In two's complement, −m is stored as $2^n - m$. An n-bit adder naturally works modulo $2^n$: any carry out of the top bit is simply lost. So adding the pattern for −m to a number x gives $x + 2^n - m$, which modulo $2^n$ is exactly $x - m$. The adder doesn't need to know whether the numbers are signed; the arithmetic comes out right as long as the result fits in the range.
:::
:::

::: practice

::: check
given: n = 0..15 int
answer: n
base: 2
width: 4

## {n} as 4 bits?

::: misses
- [any] Powers of 2: 8, 4, 2, 1.
:::
:::

::: check
given: n = 0..255 int
answer: n
tolerance: 0

## 0x{n:hex2} in decimal?

::: misses
- [any] First digit × 16 + second.
:::
:::

::: check
given: b = 4 | 8 | 16 | 32
answer: 2^(b - 1) - 1
tolerance: 0

## The largest positive number in {b}-bit two's complement?

::: misses
- [2^b - 1] That's the unsigned maximum.
- [2^(b - 1)] One less: $2^{b - 1} - 1$.
- [any] $2^{b-1} - 1$.
:::
:::
:::
