Day 15: Number systems: binary, hex, and two’s complement
The language of digital
Hardware stores everything in binary. Hexadecimal is just a human shorthand — one hex digit packs exactly four bits (a nibble), so 1010 1100 reads as 0xAC. Fluency at converting binary ↔ hex ↔ decimal in your head is non-negotiable; you'll read register dumps and instruction encodings constantly from Stage 1 on.
Two's complement: signed numbers done right
How do you represent negative numbers in bits? Two's complement is the near-universal answer: to negate a number, invert every bit and add 1. The top bit becomes a sign bit, and — the crucial property — ordinary binary addition *just works* across positive and negative values, so a CPU needs only one adder for both add and subtract (subtract = add the negation).
+5 = 0000 0101
invert -> 1111 1010
+1 -> 1111 1011 = -5
Check: 5 + (-5)
0000 0101
+ 1111 1011
= 1 0000 0000 -> drop the carry -> 0000 0000 = 0 ✓
8-bit range: -128 (1000 0000) .. +127 (0111 1111)
Sign bit = MSB. Sign-extend by copying the MSB into new high bits.Not throwaway — this is RV32I
RISC-V (Stage 1) stores signed integers in two's complement, sign-extends immediates, and flags overflow when a sign-bit carry is inconsistent. Every bit you understand here you will wire up in the ChipX ALU (Stage 2). The add/sub sharing one datapath is this idea in silicon.
Key terms
- LSB / MSB
- Least/most significant bit; in two’s complement the MSB doubles as the sign bit.
- Nibble
- Four bits = one hexadecimal digit.
- Two's complement
- Signed representation where negation = invert-all-bits + 1; enables one adder for add and subtract.
- Sign extension
- Widening a signed value by copying its MSB into the new high bits, preserving its value.
- Overflow
- A signed result that exceeds the representable range, detected from inconsistent sign-bit carries.
Before moving on, you should be able to
In 8-bit two’s complement, what is the representation of −6?