Binary and Computational Thinking
Upon completion of this topic, you will be able to:
Understand why computers use binary, convert between decimal and binary, and explain how text and colors are represented internally.
Why do computers use only 0 and 1?
The core component of a computer, the transistor, has only two states: either current flows through it (1) or it doesn't (0). Billions of transistors are combined to form a CPU, and each represents either 0 or 1.
Humans use base-10 because we have ten fingers. Computers use base-2 because they have two states. It's that simple.
Decimal vs. Binary
In decimal, 254 is:
2 Γ 10Β² + 5 Γ 10ΒΉ + 4 Γ 10β°
= 200 + 50 + 4
= 254In binary, 11111110 is:
1Γ2β· + 1Γ2βΆ + 1Γ2β΅ + 1Γ2β΄ + 1Γ2Β³ + 1Γ2Β² + 1Γ2ΒΉ + 0Γ2β°
= 128 + 64 + 32 + 16 + 8 + 4 + 2 + 0
= 254It's the same principle. Instead of powers of 10, each digit represents a power of 2.
Conversion with Python
# Decimal to binaryprint(bin(254)) # '0b11111110'print(bin(42)) # '0b101010'
# Binary to decimalprint(int('11111110', 2)) # 254print(int('101010', 2)) # 42
# Hexadecimal (a shorthand for binary)print(hex(254)) # '0xfe'print(int('fe', 16)) # 2540b is a prefix that indicates binary, and 0x indicates hexadecimal.
Bits and Bytes
- Bit: 0 or 1, the smallest unit of information
- Byte: 8 bits = 1 byte
1 bit β 2 values (0 or 1)
2 bits β 4 values (00, 01, 10, 11)
3 bits β 8 values (000 ~ 111)
8 bits β 256 values (00000000 ~ 11111111)The number of values that can be represented with n bits is 2βΏ.
| Unit | Size | Approximate Use |
|---|---|---|
| 1 byte | 8 bits | 1 English character |
| 1 KB | 1,024 bytes | Short text file |
| 1 MB | 1,024 KB | High-resolution photo |
| 1 GB | 1,024 MB | Movie (SD) |
| 1 TB | 1,024 GB | Large hard drive |
Binary Representation of Text - ASCII and Unicode
Computers don't have a concept of "characters." Every character is mapped to a number.
ASCII - 128 English characters
# Character to ASCII codeprint(ord('A')) # 65print(ord('a')) # 97print(ord('0')) # 48print(ord(' ')) # 32
# ASCII code to characterprint(chr(65)) # 'A'print(chr(97)) # 'a'print(chr(48)) # '0''A' = 65 = 01000001 (binary)
'B' = 66 = 01000010
'a' = 97 = 01100001
'0' = 48 = 00110000ASCII represents 128 characters with 7 bits β uppercase and lowercase English letters, numbers, and special characters. However, it cannot represent Korean, Japanese, or Chinese characters.
Unicode - Characters from around the world
print(ord('κ°')) # 44032print(ord('ν')) # 54620print(hex(ord('κ°'))) # '0xac00'
print(chr(44032)) # 'κ°'print(chr(127829)) # 'π' (pizza emoji)
# UTF-8 encoding - check number of bytesprint('A'.encode('utf-8')) # b'A' β 1 byteprint('κ°'.encode('utf-8')) # b'\xea\xb0\x80' β 3 bytesprint('π'.encode('utf-8')) # b'\xf0\x9f\x8d\x95' β 4 bytesUnicode includes over 140,000 characters. UTF-8 is a way to store Unicode, using 1 byte for English, 3 bytes for Korean, and 4 bytes for emojis.
Binary Representation of Colors - RGB
All colors on a screen are combinations of three values: red (R), green (G), and blue (B). Each value ranges from 0 to 255 (1 byte).
# RGB values to hexadecimal color coder, g, b = 255, 128, 0hex_color = f"#{r:02x}{g:02x}{b:02x}"print(hex_color) # #ff8000 (orange)
# Hexadecimal to RGBhex_str = "ff8000"r = int(hex_str[0:2], 16) # 255g = int(hex_str[2:4], 16) # 128b = int(hex_str[4:6], 16) # 0| Color | R | G | B | Hexadecimal |
|---|---|---|---|---|
| Red | 255 | 0 | 0 | #ff0000 |
| Green | 0 | 255 | 0 | #00ff00 |
| Blue | 0 | 0 | 255 | #0000ff |
| White | 255 | 255 | 255 | #ffffff |
| Black | 0 | 0 | 0 | #000000 |
When you write #ff8000 in CSS, this represents R=255, G=128, B=0 in hexadecimal. Each channel is 8 bits Γ 3 = 24 bits, allowing for approximately 16.7 million colors.
How to represent negative numbers - Two's Complement
Binary only has 0 and 1, so how do you represent negative numbers? Computers use Two's Complement.
With 8 bits, let's look at 5 and -5:
5 = 00000101
-5 = 11111011To create the two's complement: flip all the bits (0 β 1, 1 β 0) and then add 1.
5 = 00000101
flip = 11111010
+1 = 11111011 β This is -5# Check in Pythonprint(bin(5)) # 0b101print(bin(-5 & 0xff)) # 0b11111011 (8-bit)
# Advantage of two's complement: you can use one adder circuit for both addition and subtraction# 5 + (-5) = 00000101 + 11111011 = 100000000# Discard the 9th bit β 00000000 = 0 (correct!)Because of this, CPUs can perform subtraction using only an addition circuit, without needing a separate subtraction circuit.
The range of values that can be represented with 8-bit two's complement: -128 to +127 (a total of 256 values).
Bitwise Operations - Direct manipulation of 0 and 1
In programming, there are operations that manipulate bits directly.
a = 0b1100 # 12b = 0b1010 # 10
# AND - 1 if both are 1print(bin(a & b)) # 0b1000 (8)
# OR - 1 if at least one is 1print(bin(a | b)) # 0b1110 (14)
# XOR - 1 if differentprint(bin(a ^ b)) # 0b0110 (6)
# NOT - Invertprint(bin(~a & 0xf)) # 0b0011 (3)
# Shift - Move bits left or rightprint(a << 1) # 24 (12 * 2)print(a >> 1) # 6 (12 / 2)Left shift (<<) has the effect of multiplying by 2, and right shift (>>) has the effect of dividing by 2. Because these are faster than multiplication/division, they are used in low-level code where performance is critical.
In practice - Using bit flags for permission management
Linux file permissions (chmod 755) work with bitwise operations.
READ = 0b100 # 4WRITE = 0b010 # 2EXECUTE = 0b001 # 1
# Set permissions (combine with OR)user_perm = READ | WRITE | EXECUTE # 0b111 = 7group_perm = READ | EXECUTE # 0b101 = 5other_perm = READ | EXECUTE # 0b101 = 5
print(f"chmod {user_perm}{group_perm}{other_perm}") # chmod 755
# Check permissions (check with AND)has_write = bool(user_perm & WRITE) # Truehas_write_g = bool(group_perm & WRITE) # FalseThe meaning of chmod 755: owner (7=rwx), group (5=r-x), others (5=r-x). Each number is the decimal representation of 3 bits.
Key Takeaways
| Concept | Summary |
|---|---|
| Binary | Represents all numbers using 0 and 1. The fundamental language of computers. |
| Bit | The smallest unit of information (0 or 1) |
| Byte | 8 bits. A value in the range of 0 to 255. |
| ASCII | Maps English characters to numbers (7 bits, 128 characters) |
| Unicode | Characters from around the world (over 140,000). Stored with UTF-8. |
| RGB | Represents colors with three values (0-255). 24 bits = 16.7 million colors. |
You may not directly calculate in binary while programming. However, understanding that "computers represent everything as numbers, and those numbers as 0s and 1s" will make concepts like file size, encoding errors, color codes, and bit flags more intuitive. Understanding two's complement will explain why integer overflows occur, and understanding bitwise operations will explain why permission systems (chmod) use 777.