Show contents
Chapter 1 of 7 · 20 min
Binary and numbers
By the end of this chapter, you'll be able to
- Explain what a bit and a byte are
- Convert a number between bases 10, 2 and 16
- Write the algorithm that converts to binary
- Recognise the limits of numbers in a machine: overflow and rounding
Bits and bytes
A bit (binary digit) is the smallest possible piece of information: 0 or 1. Electrically, it is a current that flows or not, a charge present or absent. With several bits, the possibilities multiply: 2 bits give 4 combinations (00, 01, 10, 11), 3 bits give 8, and n bits give 2ⁿ.
A byte groups 8 bits: 2⁸ = 256 possible values, from 0 to 255. It is the basic unit of memory and files: a kilobyte (kB) is 1,000 bytes, a megabyte (MB) a million.
for bits in (2, 8, 16, 32):
print(f"{bits:>2} bits: {2 ** bits} values")
2 bits: 4 values
8 bits: 256 values
16 bits: 65536 values
32 bits: 4294967296 values
Counting in binary
In base 10, each digit is worth 10 times more than the one on its right: 2026 = 2×1000 + 0×100 + 2×10 + 6×1. In base 2, it is the same with powers of 2: 1011 is 1×8 + 0×4 + 1×2 + 1×1 = 11.
Python can convert both ways:
print(bin(11))
print(int("1011", 2))
print(format(2026, "b"))
print(format(5, "08b"))
0b1011
11
11111101010
00000101
bin adds the prefix 0b, which marks binary in Python code. format(n, "08b") writes n in binary on 8 digits, padded with zeros: this is how a byte is represented.
To convert by hand, you divide by 2 as long as possible: the remainders, read from bottom to top, form the binary number.
def to_binary(n):
if n == 0:
return "0"
digits = ""
while n > 0:
digits = str(n % 2) + digits
n = n // 2
return digits
print(to_binary(11), to_binary(2026), to_binary(255))
1011 11111101010 11111111
Result: 1011, that is 8 + 2 + 1 = 11.
Text description of the animation
- We start from 11 and an empty string.
- 11 divided by 2 gives 5, remainder 1: the rightmost bit is 1.
- 5 divided by 2 gives 2, remainder 1: we add it on the left.
- 2 divided by 2 gives 1, remainder 0.
- 1 divided by 2 gives 0, remainder 1. n is 0: the loop stops.
- Result: 1011, that is 8 + 2 + 1 = 11.
Hexadecimal
Binary quickly becomes unreadable. Base 16 (hexadecimal) is often used instead, with the digits 0 to 9 then the letters A to F (A = 10, …, F = 15). One hexadecimal digit matches exactly 4 bits, so a byte is written with 2 digits. You meet it in web colours: #FF8800 means red = FF (255), green = 88 (136), blue = 00.
print(hex(255), int("FF", 16), int("88", 16))
colour = "#FF8800"
red, green, blue = (int(colour[i:i + 2], 16) for i in (1, 3, 5))
print(red, green, blue)
0xff 255 136
255 136 0
The limits of numbers in a machine
In most languages, an integer takes a fixed number of bits. When the result goes beyond the available space, it “wraps around”, like a car's odometer: this is an overflow. Python has unlimited integers, but a byte can be simulated with a modulo:
byte = (200 + 100) % 256
print(byte)
44
200 + 100 = 300 does not fit in a byte (maximum 255): what is left is 300 − 256 = 44. In C or Java, this kind of integer overflow produces wrong results, or even undefined behaviour, without any error message. Several famous bugs came from this.
Mini-lab: the converterPython 3.13
Output
Click “Run”: Python will load in your browser (about 10 MB the first time).Your code runs in your browser, isolated from the page. · Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
Your turn
Worked exercise
Worked exercise: the reverse conversion
Write a function from_binary(text) that converts a string such as "1011" to a number, without using int(text, 2), going through the digits from left to right. Test it on "1011", "11111111" and "11111101010", and check the result with int.
Hint 1
Hint 2
value = value * 2 + int(digit).Show the solution and common mistakes
def from_binary(text):
value = 0
for digit in text:
value = value * 2 + int(digit)
return value
for t in ("1011", "11111111", "11111101010"):
print(t, from_binary(t), from_binary(t) == int(t, 2))
1011 11 True
11111111 255 True
11111101010 2026 True
For "1011": 0 → 1 → 2 → 5 → 11. It is the same idea as in base 10, where reading “2026” digit by digit gives 2 → 20 → 202 → 2026.
Common mistakes:
- Writing
value + int(digit) * 2: you add instead of shifting, and the result is wrong from the second digit. - Forgetting to convert
digitwithint:"1"is a character, not a number, and the addition fails with aTypeError.
Summary
- 1A bit is 0 or 1; a byte groups 8 bits, that is 256 values.
- 2In binary, each place is a power of 2; you convert by successive divisions by 2.
- 3Hexadecimal writes 4 bits per digit: a byte fits in 2 digits, as in #FF8800.
- 4A fixed-size integer can exceed its capacity and “wrap around”.
- 5Floats are approximations: compare them with math.isclose, never with ==.
Your progress is saved on this device. Create a free account to keep your progress on all your devices.