Week 1 · Foundation

The computer and the basics of programming

Before we learn to program, let's answer one question: how does a computer actually «think»? In this lesson we will interactively explore the binary system (0 and 1), how text and color are stored, and how the code we write turns into a language the computer understands, that is, compilation.

1.1 What is programming?

In programming we tell the computer what to do step by step. The computer does not figure anything out by itself; it carries out your instructions precisely, in order, and without any guessing.

Imagine you are explaining to a friend how to brew tea. For a person, saying «brew tea» is enough. But a computer is not that smart, so you have to tell it like this:

  • Pour water into the kettle
  • Heat the water until it boils
  • Add the tea leaves and wait 5 minutes
  • Pour it into the cup

This sequence of precise instructions is called an algorithm, and writing it in a computer language is called a program. In this course we will learn to write such instructions. But first we need to understand how the computer «hears» these instructions.

1.2 The computer and two states: 0 and 1

Inside a computer there are millions of very small «switches» (transistors). Each switch has only two states: there is current or there is no current. We mark them like this:

  • 0 means no current (off)
  • 1 means there is current (on)

One such 0 or 1 is called a bit (the smallest piece of data). Eight bits together make a byte. Everything in a computer, that is, numbers, letters, images, video and music, is ultimately just a collection of 0s and 1s.

A question arises: how can just 0 and 1 store large numbers, a whole book or a color image? The answer is in the binary system below. Let's try it by hand.

1.3 The binary number system

In everyday life we use the decimal system, which has 10 digits (from 0 to 9). The computer, however, uses the binary system, which has only 2 digits: 0 and 1. Don't worry, the secret of both is one simple idea: the position each digit sits in matters.

First, something familiar: a decimal number

Let's take the number «342». In fact we «assemble» it this way every day, we just don't notice it. Each digit is multiplied by its place value:

hundreds
3
tens
4
ones
2
= 342

3×100 + 4×10 + 2×1 = 300 + 40 + 2 = 342

Notice: from right to left each place grows 10 times larger: ones, tens, hundreds...

Now the same idea in binary

Binary is exactly the same, only each place grows not 10 but 2 times larger: 1, 2, 4, 8, 16, 32, 64, 128. One byte (8 bits) consists of these eight places. The rule is simple: the place value where a 1 sits is added, and where a 0 sits it is not added.

For example, let's «read» the number 00001101 together:

128
0
64
0
32
0
16
0
8
1
4
1
2
0
1
1
= 13

We add only the on (1) places: 8 + 4 + 1 = 13

Now it's your turn. In the game below, click the bits and watch with your own eyes how the number is assembled:

Play by clicking the bits turn a bit on/off
Result: 0
0b00000000

See it? Each on bit adds its own «place value». For example, 01000001 = 64 + 1 = 65. With one byte you can express 0 to 255, that is 256 different numbers in total.

«Why do I need this, I'm going to write code?» That's a fair question. Relax: when coding you will not write 0 and 1 by hand. But if you know this foundation, then later memory, data types, colors and errors will not feel like «magic» to you; you will understand what is happening. This is exactly what sets a strong programmer apart from the rest.

Binary ↔ Decimal converter

Write any number, and its binary form will appear on the other side (and vice versa).

Converter

1.4 Units of data: byte, KB, MB, GB

One byte is almost nothing, just a single letter. But bytes add up and form enormous things. You've seen the label «128 GB of storage» on your phone; that GB is also actually made of bytes. Each next unit is about 1000 times larger than the previous one:

1 byte1 letter
×1000
1 KBshort message
×1000
1 MB1 image
×1000
1 GB~30 min video
×1000
1 TBthousands of movies

So file size («3 MB image»), storage («256 GB»), internet speed («50 Mbit/s»), all of these are measured with these bytes. Now, when you see these numbers, they will be meaningful to you.

For precision: because the computer is binary, 1 KB = 1024 bytes (2¹⁰), not 1000. But in everyday life, for convenience, it is rounded to «about 1000».

1.5 How are a letter, color and hex stored?

If a byte can store a number in the range 0–255, then it can also store letters: we simply assign one number to each letter. For example, Latin A = 65, B = 66, a = 97, space = 32.

The question «why exactly 65?» comes up naturally. The answer is simple: it is a table the whole world agreed on (like a shared dictionary), and its name is ASCII. Because everyone follows this table, text written on one computer can be read the same way by another.

Write a word below, and you will see the number behind each letter and its 8-bit binary form:

ASCII inspector

A color is also three numbers

The color of each point (pixel) on the screen consists of three bytes: how much red, how much green, how much blue (each 0–255). This is called RGB.

An important point: this is not paint but a mixture of light (the screen emits light). So it is the opposite of paint: if all three are 0, there is no light, that is black; if all three are 255, there is full light, that is white. Drag the sliders and see how a color is «born from numbers»:

RGB color mixer
R9101011011
G9101011011
B21411010110
#5B5BD6

What about Uzbek? Unicode

The ASCII table has only 128 characters: Latin letters, digits and punctuation. But there are thousands of characters in the world: Cyrillic letters, Uzbek oʻ and gʻ, Chinese characters, even the emoji 😀. To fit all of them, there is a larger table called Unicode. In it a single character can take not 1 but several bytes (the most widespread form is UTF-8).

Try writing an emoji or a Cyrillic letter in the ASCII inspector above, and its code will come out greater than 255, meaning it does not fit in one byte.

Hex: the short language of numbers

In the color result you saw a notation like #5B5BD6. This is the hexadecimal (hex) system. It has 16 «digits»: 0–9, then A, B, C, D, E, F (that is 10, 11, 12, 13, 14, 15). Why is it needed? Because 2 hex characters represent exactly 1 byte (8 bits), which is very compact. For example, FF = 255 (the largest byte), 5B = 91. That is why colors are written in hex: #RRGGBB, that is the byte values of red, green and blue.

So text, image, video and music are all numbers, and numbers are bits (0 and 1). The entire «magic» of the computer is based on this simple idea.

1.6 Compilation: translating code into machine language

We write a program in a language a human can read. For example, in C a program that prints «Salom, dunyo!» looks like this:

// salom.c (code a human writes) #include <stdio.h> int main(void) { printf("Salom, dunyo!\n"); }

But the processor does not understand this text, it only knows binary machine code (0 and 1). For this reason a «translator» is needed in between; it is called a compiler (the famous one for C is gcc). The compiler reads your code in full and turns it into a binary file the processor can execute.

Click the button below and watch which stages the code goes through:

The compilation process
salom.c
human code
gcc
compiler
salom
machine code · 01001
Run
the program runs
$ press the button...

What if you make a mistake?

The compiler is very demanding: if there is an error in the code (for example, a missing semicolon), it will not build the program at all and will tell you exactly which line the error is on. This is a good thing: the error is found before the program runs. For example:

salom.c:4:31: error: expected ';' before '}' token

This message means «on line 4, a ; was forgotten». In the coming weeks we will learn to read and fix such errors.

Compilation vs interpretation

Not every language is compiled. Languages like C are converted fully into machine code in advance (compilation), so they run fast. Languages like Python, on the other hand, are read line by line by an interpreter at run time (there is no separate compilation step); this is convenient but slower. We learn the basics in C: it shows most openly what is happening inside the computer.

That is why C is called a «compiled language». If you change the code, you have to compile it again. In the coming weeks we will write real C code and compile it in the browser.

1.7 Practice exercise: solve it yourself

The best way to lock in knowledge is to do it yourself. In this exercise one number appears each time, and one of two cases occurs:

  • If the number consists only of 0s and 1s (binary), convert it to a decimal number.
  • If the number is ordinary (decimal), convert it to 8-bit binary.

When solving, use the helper below; these are the place values. When reading binary, add the values of the on (1) places. For example: 1010 = 8 + 2 = 10; and the other way, 10 = 00001010. Write your answer and press Enter (or «Check»). The goal: collect 10 correct answers.

Practice machine 0 / 10 correct
Helper (place values):1286432168421
🎉 Great! 10 correct answers. You've now firmly mastered the binary system!

1.8 Going deeper: computing with binary advanced

This section is deeper and optional. It takes you from «storing data» to «how the computer computes». Real understanding begins here.

Addition in binary

We add as in decimal: 0+0=0, 0+1=1, and 1+1 in binary is «10», meaning you write 0 and carry the 1 to the next place (just like the «carry» in decimal at 9+1=10). Example: 3 + 1 = 4:

0011 (3) + 0001 (1) ------ 0100 (4)

The tiny circuits inside the processor perform exactly this operation billions of times per second. This is the computer's «computing».

Negative numbers and two's complement

With only 0 and 1 there is no «minus» sign. The trick is this: the leftmost bit is used as the sign, and negative numbers are written in a special way, that is, with two's complement. The beauty of this trick: the processor computes both +5 and −5 with the same addition circuit, so no separate subtraction is needed. We will see the details in the coming weeks.

Bit operations (bitwise)

Sometimes a programmer operates directly on bits. The main ones: AND (if both are 1 → 1), OR (if one is 1 → 1), XOR (if exactly one is 1 → 1), NOT (swaps 0 and 1), shift (moves the bits left/right). Drag the sliders and try the operations:

Bit operations lab
A0101101090
B0011110060
Result: 00011000
= 24 (decimal)

Logic gates: everything comes from these

These operations are performed inside the processor by tiny electronic circuits called logic gates (AND, OR, NOT). From them an «adder» is built, and from adders an entire processor is assembled. So a computer consists of millions of AND/OR/NOT gates. This is how the entire digital world is built from just 0 and 1.

Don't worry if you didn't fully understand this section, we will come back to it again in the coming weeks. The main thing is that you now know «a computer is not a magic box, it is a machine that computes with 0 and 1».

1.9 Knowledge quiz

Before the quiz: new terms

The main concepts learned this week. Skim through them before the quiz:

Bitthe smallest piece of data, 0 or 1.
Byte8 bits; one number in the range 0–255.
Binary systema number system made only of 0 and 1.
Place valuethe position a digit sits in (1, 2, 4, 8...).
ASCIIa shared table that assigns a number to each letter.
RGBcolor = red+green+blue (each 0–255).
Machine codebinary instructions the processor understands.
Compilertranslates human code into machine code (gcc).

Now let's try it

15 questions. To complete the week, answer at least 11 correctly.

Congratulations! Week 1 is complete

You now know that inside a computer everything is 0 and 1, how numbers, letters and colors are stored, and what compilation is. This is a solid foundation for programming.