Digital Logic Toolkit — Numbers & Codes
Number systems and error-correcting codes
A computer stores one thing — bits — and every number, character and code in this section is an agreement about what a run of bits means. These ten tools convert between those agreements and show the arithmetic that gets you from one to the other.
Representing numbers
Binary ⇄ decimal ⇄ hex ⇄ octal
Convert between any two bases, integers and fractions, with the positional working.
Typical input13.625 → 1101.101₂ = 15.5₈ = D.A₁₆ →
Binary ⇄ text / ASCII
Convert text to binary and back, byte by byte, with the full UTF-8 breakdown.
Typical inputHello → 48 65 6C 6C 6F →
Two’s complement
Encode and decode signed binary at any width, with overflow and sign extension.
Typical input11010110 at width 8 → −42 →
Binary arithmetic
Add, subtract, multiply and divide in binary with every carry and borrow shown.
Typical input0111 1111 + 0000 0001 → 1000 0000, carry-out 0, signed overflow →
Gray code
Convert binary to reflected Gray code and back, and build the sequence by reflection.
Typical input00001011 → Gray 00001110 →
BCD converter
Pack and unpack binary-coded decimal, and add with the +6 correction shown.
Typical input27 + 45 → 72, one +0110 correction in the units digit →
Detecting and correcting errors
Hamming code
Encode, inject an error, and watch the syndrome point straight at the flipped bit.
Typical inputdata 1011 → (7,4) codeword 0110011 →
Parity & checksum
Even and odd parity, one’s-complement sums and the Internet checksum, step by step.
Typical input1011001 → even parity bit 0, odd parity bit 1 →
Huffman coding
Build the tree from text or from frequencies, with the merge order and canonical codes.
Typical inputMISSISSIPPI → 21 bits instead of 33 →
Shannon entropy
Bits per symbol for any distribution, with the surprisal of each symbol shown.
Typical inputMISSISSIPPI → H = 1.8231 bits/symbol →
The three signed representations
Every one of these tools eventually asks the same question: what does this run of bits stand for? Nowhere is the answer more divided than for negative numbers, where three conventions are all still taught and only one of them is used by machines.
| Representation | Bits | Range | Zeros |
|---|---|---|---|
| Sign–magnitude | 1000 0101 | -127…127 | two (0000 0000, 1000 0000) |
| One's complement | 1111 1010 | -127…127 | two (0000 0000, 1111 1111) |
| Two's complement | 1111 1011 | -128…127 | one |
Only two's complement lets ordinary binary addition work unmodified, which is why every machine you will ever use picks it. One's complement needs an end-around carry — the same mechanism as the Internet checksum. Two’s complement and parity & checksum each show the arithmetic.
Related
- Boolean & Minimisation
Turn one expression into every other form of itself, and reduce it with named laws.
- Gates & Circuits
What each gate does, in both symbol standards, and how to build circuits out of them.