Every value inside a computer — numbers, letters, images, code — is ultimately stored as binary: a sequence of 1s and 0s. Hardware uses two voltage levels (high/low) to represent these bits because two-state systems are extremely reliable and cheap to manufacture.
A single binary digit is a bit. Eight bits form a byte. From bytes we build kilobytes (210), megabytes (220), gigabytes (230), and so on.
Each bit position is a power of 2, starting at 20 on the right.
Hex (base-16) compresses binary: each hex digit maps to exactly 4 bits. Digits 0–9 then A=10, B=11, C=12, D=13, E=14, F=15.
| Hex | Binary | Decimal |
|---|---|---|
| 0 | 0000 | 0 |
| 9 | 1001 | 9 |
| A | 1010 | 10 |
| F | 1111 | 15 |
0x2F = 0010 11112 = 2×16 + 15 = 47
To represent negative integers, computers use two's complement. For an n-bit number, the most-significant bit (MSB) has a weight of −2n−1 instead of +2n−1.
Range for 8-bit signed: −128 to +127. The bit pattern 1000 0000 = −128 (no positive counterpart — this is why overflow matters).
Convert 1101 01102 to decimal. Show each step.
Convert 255 to hexadecimal.
A 16-bit signed integer: what is the most negative value it can store?
What is 1010 1010₂ in decimal?
When reading binary, list all bit positions where the digit is 1, then sum their powers of 2. For 1010 1010: positions 7,5,3,1 → 128+32+8+2 = 170.
It's easy to miss the low-order bits. Always write out all 8 positions and check each one: 1×128 + 0×64 + 1×32 + 0×16 + 1×8 + 0×4 + 1×2 + 0×1 = 170.
255 = 1111 1111. In this number, alternating bits are 0, so the total is lower. Re-check which positions have a 1.
To negate a two's complement number you:
Flipping only the MSB is the sign-magnitude approach, not two's complement. Two's complement: flip ALL bits, then add 1. This ensures addition still works correctly in hardware.
Adding 1 just gives you n+1, not −n. The full process is: invert every bit, then add 1.
Hardware negation is a bit operation, not arithmetic division. Flip all bits, add 1 — then the adder circuits can handle subtraction as addition of a negative.
0xFF in decimal is:
0x100 = 1×256 = 256. 0xFF = 15×16 + 15 = 255. Remember: F = 15, so FF = 15×16 + 15.
0xF0 = 15×16 + 0 = 240. But 0xFF = 15×16 + 15 = 240 + 15 = 255.
0x80 = 8×16 + 0 = 128. 0xFF is much larger — all 8 bits set = 255.
An 8-bit unsigned integer can represent values from 0 to:
2⁷=128 is the range for 7 bits. For 8 bits: 2⁸ = 256 distinct values → 0 to 255.
There are 2⁸=256 distinct patterns, but they represent 0 through 255. Maximum value = 256−1 = 255.
9-bit range: 0–511. 8-bit unsigned: 0–255.
Hexadecimal is useful in computing primarily because:
Hex has digits 0–9 plus A–F (16 total). The benefit isn't fewer symbols but a cleaner relationship to binary.
All CPU arithmetic is binary. Hex is just a human-readable shorthand — it doesn't change what the CPU does.
Two's complement works the same whether you write numbers in decimal, hex, or binary. Hex's advantage is compact binary representation.