Back to List

Binary and Computational Thinking

Understand why computers use only 0 and 1, the meaning of bits and bytes, and the binary representation of text and colors.

Beginner
|
10min
|
Verified (2026-07)
binarybitbytebinary numbercomputer representation
Progress0/23 (0%)

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:

text
2 Γ— 10Β² + 5 Γ— 10ΒΉ + 4 Γ— 10⁰
= 200   + 50     + 4
= 254

In binary, 11111110 is:

text
1Γ—2⁷ + 1Γ—2⁢ + 1Γ—2⁡ + 1Γ—2⁴ + 1Γ—2Β³ + 1Γ—2Β² + 1Γ—2ΒΉ + 0Γ—2⁰
= 128 + 64   + 32   + 16   + 8    + 4    + 2    + 0
= 254

It's the same principle. Instead of powers of 10, each digit represents a power of 2.

Conversion with Python

python
# Decimal to binary
print(bin(254)) # '0b11111110'
print(bin(42)) # '0b101010'
# Binary to decimal
print(int('11111110', 2)) # 254
print(int('101010', 2)) # 42
# Hexadecimal (a shorthand for binary)
print(hex(254)) # '0xfe'
print(int('fe', 16)) # 254

0b 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
text
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ⁿ.

UnitSizeApproximate Use
1 byte8 bits1 English character
1 KB1,024 bytesShort text file
1 MB1,024 KBHigh-resolution photo
1 GB1,024 MBMovie (SD)
1 TB1,024 GBLarge 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

python
# Character to ASCII code
print(ord('A')) # 65
print(ord('a')) # 97
print(ord('0')) # 48
print(ord(' ')) # 32
# ASCII code to character
print(chr(65)) # 'A'
print(chr(97)) # 'a'
print(chr(48)) # '0'
text
'A' = 65 = 01000001 (binary)
'B' = 66 = 01000010
'a' = 97 = 01100001
'0' = 48 = 00110000

ASCII 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

python
print(ord('κ°€')) # 44032
print(ord('ν•œ')) # 54620
print(hex(ord('κ°€'))) # '0xac00'
print(chr(44032)) # 'κ°€'
print(chr(127829)) # 'πŸ•' (pizza emoji)
# UTF-8 encoding - check number of bytes
print('A'.encode('utf-8')) # b'A' β€” 1 byte
print('κ°€'.encode('utf-8')) # b'\xea\xb0\x80' β€” 3 bytes
print('πŸ•'.encode('utf-8')) # b'\xf0\x9f\x8d\x95' β€” 4 bytes

Unicode 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).

python
# RGB values to hexadecimal color code
r, g, b = 255, 128, 0
hex_color = f"#{r:02x}{g:02x}{b:02x}"
print(hex_color) # #ff8000 (orange)
# Hexadecimal to RGB
hex_str = "ff8000"
r = int(hex_str[0:2], 16) # 255
g = int(hex_str[2:4], 16) # 128
b = int(hex_str[4:6], 16) # 0
ColorRGBHexadecimal
Red25500#ff0000
Green02550#00ff00
Blue00255#0000ff
White255255255#ffffff
Black000#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:

text
5 = 00000101
-5 = 11111011

To create the two's complement: flip all the bits (0 β†’ 1, 1 β†’ 0) and then add 1.

text
5    = 00000101
flip   = 11111010
+1    = 11111011  β†’  This is -5
python
# Check in Python
print(bin(5)) # 0b101
print(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.

python
a = 0b1100 # 12
b = 0b1010 # 10
# AND - 1 if both are 1
print(bin(a & b)) # 0b1000 (8)
# OR - 1 if at least one is 1
print(bin(a | b)) # 0b1110 (14)
# XOR - 1 if different
print(bin(a ^ b)) # 0b0110 (6)
# NOT - Invert
print(bin(~a & 0xf)) # 0b0011 (3)
# Shift - Move bits left or right
print(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.

python
READ = 0b100 # 4
WRITE = 0b010 # 2
EXECUTE = 0b001 # 1
# Set permissions (combine with OR)
user_perm = READ | WRITE | EXECUTE # 0b111 = 7
group_perm = READ | EXECUTE # 0b101 = 5
other_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) # True
has_write_g = bool(group_perm & WRITE) # False

The 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

ConceptSummary
BinaryRepresents all numbers using 0 and 1. The fundamental language of computers.
BitThe smallest unit of information (0 or 1)
Byte8 bits. A value in the range of 0 to 255.
ASCIIMaps English characters to numbers (7 bits, 128 characters)
UnicodeCharacters from around the world (over 140,000). Stored with UTF-8.
RGBRepresents 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.

πŸ’¬ Questions & Comments

0 comments

You can post without signing in. Guest comments cannot be edited or deleted by their author.

0/2000

Loading...