Computational Thinking and Foundations
Build core mental models for problem solving, data representation, and the computing ecosystem.
Content
Number Systems and Conversions
Versions:
Watch & Learn
AI-discovered learning video
Sign in to watch the learning video for this topic.
Number Systems and Conversions — Level Up from Bits & Bytes
You already met bits, bytes, and the binary way of life in the previous lecture, and you know computing has a colorful history. Now we stop whispering about ones and zeros and start turning them into meaningful numbers humans, machines, and web colors can actually use.
This is the moment where conversions stop being mystical and start being mechanical.
What is a number system, really?
A number system is a way to write quantities. The rule that makes a system a system is the base (or radix).
- Base (b) = how many distinct digits exist (0 through b-1).
- Decimal (base 10) uses digits 0..9. Humans love this because of ten fingers.
- Binary (base 2) uses digits 0..1. Computers love this because silicon flips.
- Hexadecimal (base 16) uses 0..9 and A..F. Programmers love this because it compresses binary into readable chunks.
Why it matters: representations affect how we store, transmit, and interpret data. Memory addresses, color codes, IPs, and file formats all rely on these conversions.
Positional notation — the rule that does the heavy lifting
A number like 342 in base 10 means:
342 = 310^2 + 410^1 + 2*10^0
In base b, digits multiply powers of b. So binary 1011 means:
1011(base 2) = 12^3 + 02^2 + 12^1 + 12^0 = 11(decimal)
Quick table: powers for small bases
| power | 2^n | 10^n |
|---|---|---|
| 0 | 1 | 1 |
| 1 | 2 | 10 |
| 2 | 4 | 100 |
| 3 | 8 | 1000 |
Converting decimal to binary (integer part) — the divide-by-base trick
Algorithm (decimal -> base b):
- Divide the number by b.
- Record the remainder (least significant digit).
- Replace number by quotient and repeat until quotient is 0.
- Read remainders bottom-up for final representation.
Example: Convert 156 to binary
- 156 / 2 = 78 remainder 0
- 78 / 2 = 39 remainder 0
- 39 / 2 = 19 remainder 1
- 19 / 2 = 9 remainder 1
- 9 / 2 = 4 remainder 1
- 4 / 2 = 2 remainder 0
- 2 / 2 = 1 remainder 0
- 1 / 2 = 0 remainder 1
Read bottom-up: 10011100
So 156(decimal) = 10011100(binary)
Converting binary to decimal — sum the powers
Algorithm (binary -> decimal): for each 1-bit, add 2^position.
Example: 1101(base 2)
= 12^3 + 12^2 + 02^1 + 12^0
= 8 + 4 + 0 + 1 = 13(decimal)
Fractional parts: multiply by base
Decimal to binary fraction (e.g., 0.625):
- Multiply fraction by 2.
- Record integer part (0 or 1). That is the next bit.
- Keep fractional part and repeat until it terminates or you reach desired precision.
Example: 0.625 * 2 = 1.25 -> bit 1, remaining 0.25
0.25 * 2 = 0.5 -> bit 0, remaining 0.5
0.5 * 2 = 1.0 -> bit 1, remaining 0.0
So 0.625 = 0.101(base 2)
Note: Some fractions do not terminate in binary (like 0.1 decimal). Welcome to rounding error land.
Hexadecimal: the human-friendly binary shorthand
Hex digits: 0 1 2 3 4 5 6 7 8 9 A B C D E F (A=10 ... F=15)
Why hex? Every hex digit maps neatly to 4 binary bits (a nibble). So conversions are easy:
- Group binary into 4-bit chunks from the right, pad with leading zeros if needed.
- Convert each group to its hex digit.
Example: Convert binary 101111001 to hex
Group: 1 0111 1001 -> pad left: 0001 0111 1001
0001 = 1, 0111 = 7, 1001 = 9
Result: 0x179
Practical uses: memory addresses (0x7ffd...), color codes (#FF33CC), and debugging dumps.
Two's complement — how negatives are actually encoded
Computers like fixed-size fields. Two's complement gives us a neat way to encode negatives with binary arithmetic that 'just works'.
To get the two's complement of a positive number:
- Write it in binary with fixed width (e.g., 8 bits).
- Invert the bits (flip 0 <-> 1).
- Add 1.
Example: Represent -13 in 8-bit two's complement
13 = 00001101
Invert = 11110010
Add 1 = 11110011
So -13 is 11110011. As an unsigned number that bit pattern is 243, but in two's complement it means -13.
Overflow: If you add two n-bit two's complement numbers and the result needs n+1 bits, the extra carry is discarded and the sign may flip unexpectedly. Always consider bit width.
Quick conversions cheat sheet
- Decimal -> Binary: divide by 2, collect remainders.
- Binary -> Decimal: sum bits times powers of 2.
- Binary -> Hex: group into 4 bits, map to hex digit.
- Hex -> Binary: expand each hex digit to 4 bits.
- Fractional decimal -> binary: multiply fraction by 2 repeatedly.
- Negative numbers: use two's complement for signed representation.
Practical examples and tiny code snippet
Why do engineers obsess over this? Because these conversions power memory addressing, color codes, bitwise tricks, and low-level debugging.
Example: color #FF33CC -> split into bytes
- FF = 255 -> red
- 33 = 51 -> green
- CC = 204 -> blue
Tiny Python helpers
# decimal to binary string
def dec_to_bin(n):
return bin(n)[2:]
# binary string to decimal
def bin_to_dec(s):
return int(s, 2)
# hex to binary
def hex_to_bin(h):
return bin(int(h, 16))[2:].zfill(len(h)*4)
Short practice problems (try these!)
- Convert 2021 decimal to binary and hex.
- Convert binary 110010101111 to decimal and hex.
- Represent -45 in 8-bit two's complement.
Answers at the bottom if you must peek.
Key takeaways
- A number system is defined by its base; positional notation weights digits by powers of the base.
- Convert integers by division or by summing powers; convert fractions by repeated multiplication.
- Hex is binary shorthand (4 bits per hex digit) and is ubiquitous in systems programming.
- Two's complement is the standard for signed integers and enables common arithmetic.
Memorable insight: Bits are tiny, but their ordering and interpretation are everything. A pattern of ones and zeros is meaningless until you choose a base and a convention.
Answers to practice problems
- 2021 = 11111100101(base 2) = 0x7E5
- 110010101111(base 2) = 3247(decimal) = 0xCAF
- 45 = 00101101 -> invert 11010010 -> add 1 = 11010011, so -45 = 11010011 (8-bit two's complement)
Tags: beginner, computer science, visual
Comments (0)
Please sign in to leave a comment.
No comments yet. Be the first to comment!