Week 2 · Explore: bits, bytes, ASCII and pixels¶
Introduction to Computer Science (YMT113) · Fall 2026
Nothing here is graded. Run each cell (Shift+Enter), read the output, change something, run it again.
Runs in Google Colab with nothing installed.
1 · Counting in binary¶
Python can show any number in binary with bin() and pad it to 8 bits with format.
for n in [0, 1, 2, 5, 73, 128, 255]:
print(f"{n:>4} → {n:08b}")
0 → 00000000 1 → 00000001 2 → 00000010 5 → 00000101 73 → 01001001 128 → 10000000 255 → 11111111
Read each row from the left: 128 64 32 16 8 4 2 1. Add the place values under the 1s and you get the number back.
bits = "01001001"
value = sum(int(b) * 2 ** (7 - i) for i, b in enumerate(bits))
print(bits, "=", value)
01001001 = 73
2 · Text is numbers: ASCII and Unicode¶
ord() gives a character's code, chr() goes back. ASCII is the first 128 codes; Unicode continues from there.
for c in "Hi!":
print(f"{c!r:5} {ord(c):4} {ord(c):08b}")
'H' 72 01001000 'i' 105 01101001 '!' 33 00100001
print(chr(72), chr(105), chr(33))
print("Türkçe:", [ord(c) for c in "Şğü"])
print("😂 is code point", ord("😂"), "and takes", len("😂".encode("utf-8")), "bytes in UTF-8")
H i ! Türkçe: [350, 287, 252] 😂 is code point 128514 and takes 4 bytes in UTF-8
Your name as bytes:
name = "Firat" # change me
data = name.encode("utf-8")
print(len(name), "characters →", len(data), "bytes →", 8 * len(data), "bits")
print(" ".join(f"{b:08b}" for b in data))
5 characters → 5 bytes → 40 bits 01000110 01101001 01110010 01100001 01110100
3 · The same bits, different meanings¶
Two bytes can be two letters, one 16-bit number, or part of a color. Bits carry no meaning; programs do.
two = "Hi".encode("utf-8")
print("as text :", two.decode("utf-8"))
print("as number :", int.from_bytes(two, "big"))
print("as color : R =", two[0], " G =", two[1], " (with B = 0 →", f"#{two[0]:02x}{two[1]:02x}00)")
as text : Hi as number : 18537 as color : R = 72 G = 105 (with B = 0 → #486900)
4 · An image is a grid of numbers¶
Each pixel is three bytes (red, green, blue). Let's draw a 8×8 smiley from a grid of 0/1 and then color it.
import numpy as np
import matplotlib.pyplot as plt
smiley = np.array([
[0,0,1,1,1,1,0,0],
[0,1,0,0,0,0,1,0],
[1,0,1,0,0,1,0,1],
[1,0,0,0,0,0,0,1],
[1,0,1,0,0,1,0,1],
[1,0,0,1,1,0,0,1],
[0,1,0,0,0,0,1,0],
[0,0,1,1,1,1,0,0],
])
print(smiley)
plt.figure(figsize=(3, 3)); plt.imshow(smiley, cmap="gray_r"); plt.axis("off"); plt.show()
[[0 0 1 1 1 1 0 0] [0 1 0 0 0 0 1 0] [1 0 1 0 0 1 0 1] [1 0 0 0 0 0 0 1] [1 0 1 0 0 1 0 1] [1 0 0 1 1 0 0 1] [0 1 0 0 0 0 1 0] [0 0 1 1 1 1 0 0]]
# now in color: every pixel is (R, G, B), 0–255
rgb = np.zeros((8, 8, 3), dtype=np.uint8)
rgb[smiley == 1] = (238, 90, 31) # orange for the 1s
rgb[smiley == 0] = (246, 246, 242) # paper for the 0s
print("one pixel:", rgb[0, 2], "→ 3 bytes")
print("whole image:", rgb.size, "bytes")
plt.figure(figsize=(3, 3)); plt.imshow(rgb); plt.axis("off"); plt.show()
one pixel: [238 90 31] → 3 bytes whole image: 192 bytes
A 1920×1080 photo is 2 073 600 pixels × 3 bytes ≈ 6.2 MB uncompressed. JPEG makes it ten times smaller by throwing away what your eye cannot see; that is an algorithm, next week's topic.
5 · Why binary search wins¶
Count the steps needed to find one item among n, page by page (linear) and by halving (binary).
import math
print(f"{'n':>15} {'linear':>15} {'binary':>8}")
for n in [1_000, 1_000_000, 1_000_000_000, 8_000_000_000]:
print(f"{n:>15,} {n:>15,} {math.ceil(math.log2(n)):>8}")
n linear binary
1,000 1,000 10
1,000,000 1,000,000 20
1,000,000,000 1,000,000,000 30
8,000,000,000 8,000,000,000 33
Eight billion people, 33 steps. That is the wow moment of week 5, seen from here.