Number Systems
Binary · Hex · 2's Complement
From counting in binary to IEEE 754 floating point — the complete foundation every digital designer must master.
Number Base Systems
| Base | Name | Digits | Verilog Prefix | Example: 42 |
|---|---|---|---|---|
| 2 | Binary | 0–1 | 'b | 00101010 |
| 8 | Octal | 0–7 | 'o | 052 |
| 10 | Decimal | 0–9 | 'd | 42 |
| 16 | Hexadecimal | 0–9, A–F | 'h | 2A |
Positional Value
Each digit position carries weight = baseposition (rightmost = position 0)
Binary 1011 = 1×2³ + 0×2² + 1×2¹ + 1×2⁰ = 8+0+2+1 = 11
Hex 0x1F = 1×16 + 15×1 = 31
BCD 0011 1001 = digit 3, digit 9 = 39
Interactive Multi-Base Converter
2's Complement (8-bit signed interpretation)
Signed Number Representations (8-bit)
| Format | +5 | −5 | Range | Zeros |
|---|---|---|---|---|
| Sign-Magnitude | 0000 0101 | 1000 0101 | −127 to +127 | Two (+0, −0) |
| 1's Complement | 0000 0101 | 1111 1010 | −127 to +127 | Two |
| 2's Complement | 0000 0101 | 1111 1011 | −128 to +127 | One ✓ |
2's Complement Arithmetic Examples
| Operation | Binary | Decimal check |
|---|---|---|
| +5 + +3 | 0000 0101 + 0000 0011 = 0000 1000 | = +8 ✓ |
| +5 + (−3) | 0000 0101 + 1111 1101 = [1]0000 0010 | = +2 ✓ (discard carry) |
| (−5) + (−3) | 1111 1011 + 1111 1101 = [1]1111 1000 | = −8 ✓ |
| +70 + +70 (overflow!) | 0100 0110 + 0100 0110 = 1000 1100 | = −116 ✗ Overflow |
Overflow detection: carry_in to MSB XOR carry_out from MSB = 1
BCD — Binary Coded Decimal
| Decimal | BCD (4-bit groups) | Pure Binary |
|---|---|---|
| 0 | 0000 | 0000 |
| 9 | 1001 | 1001 |
| 10 | 0001 0000 | 0000 1010 |
| 39 | 0011 1001 | 0010 0111 |
| 99 | 1001 1001 | 0110 0011 |
IEEE 754 Floating Point
Value = (−1)S × 2Exponent − 127 × 1.Mantissa
| Format | Total bits | Exponent | Mantissa | Approximate range |
|---|---|---|---|---|
| Half (fp16) | 16 | 5 | 10 | ±65504 |
| BF16 (brain float) | 16 | 8 | 7 | Same as float32 |
| Single (float32) | 32 | 8 | 23 | ±3.4×10³⁸ |
| Double (float64) | 64 | 11 | 52 | ±1.8×10³⁰⁸ |
Example: +13.0 → IEEE 754
// +13.0 in IEEE 754 single precision
// 13 decimal = 1101 binary = 1.101 × 2³
// Sign = 0
// Exponent = 3 + 127 = 130 = 10000010
// Mantissa = 101 + 20 zeros
// Bits: 0 10000010 10100000000000000000000
// Hex: 0x41500000
// Verilog: bit-field inspection
logic [31:0] f = 32'h41500000;
initial $display("sign=%b exp=%0d mant=%b",
f[31], f[30:23]-127, f[22:0]);
// Output: sign=0 exp=3 mant=10100000000000000000000
Verilog Number Literals & Signed Arithmetic
// Format: [width]'[base][value]
logic [7:0] a;
a = 8'd42; // decimal
a = 8'b00101010; // binary
a = 8'h2A; // hex
a = 8'o52; // octal
a = 8'hXX; // unknown (X)
a = 8'hZZ; // high-impedance (Z)
// Signed declaration
logic signed [7:0] s = -8'sd5; // -5 → 8'b11111011
// Overflow detection (2's complement)
logic [7:0] a8, b8, sum8;
logic cin, cout, overflow;
assign {cout, sum8} = a8 + b8 + cin;
assign overflow = cin ^ cout; // V flag
// BCD add with correction
function automatic [4:0] bcd_add_digit;
input [3:0] a, b; input cin;
logic [4:0] t;
t = a + b + cin;
if(t > 9) t = t + 6;
return t;
endfunction
Quick Reference — Hex to Binary
| Hex | Binary | Hex | Binary |
|---|---|---|---|
| 0 | 0000 | 8 | 1000 |
| 1 | 0001 | 9 | 1001 |
| 2 | 0010 | A | 1010 |
| 3 | 0011 | B | 1011 |
| 4 | 0100 | C | 1100 |
| 5 | 0101 | D | 1101 |
| 6 | 0110 | E | 1110 |
| 7 | 0111 | F | 1111 |
Frequently Asked Questions
How do you convert binary to 2's complement?
Invert all bits (1's complement), then add 1. For +5 = 0000 0101 → 1111 1010 → +1 → 1111 1011 = −5.
Why does hardware use 2's complement over sign-magnitude?
Same adder hardware for add and subtract, single zero representation, carry-out signals overflow directly. Three wins that make ALU design dramatically simpler.
What are BCD's use cases today?
7-segment display drivers, financial/banking systems where exact decimal rounding matters, embedded clocks/timers (RTC chips store time in BCD), legacy PLC systems.
How do you detect overflow in 2's complement addition?
Overflow = carry_in_to_MSB XOR carry_out_from_MSB. In Verilog: assign ov = cin ^ cout; Also: two positives sum to negative, or two negatives sum to positive → overflow.