How Computers Understand Code

How does a computer follow our instructions?

A teacher writes a small program to count students. The program uses readable names such as students, but the processor does not recognize school vocabulary. Something must connect those written instructions to the operations the hardware can perform.

Source code is the text we write. Language software reads its structure and arranges for its instructions to be carried out. The processor ultimately executes machine instructions. “Understand” here means follow defined rules; the computer does not understand the teacher’s intention in the human sense.

  1. Explain the journey

    Describe the roles of source code, language software and the processor without treating them as the same thing.

  2. Read binary data

    Use binary place values to explain a small student count.

  3. Predict a change

    Trace what happens when one student joins, and distinguish a changed value from a changed program.

From a teacher’s rule to a displayed count

  1. Source code

    The teacher states a counting rule in a programming language.

    students + 1
  2. Language software

    A language engine reads the code and prepares or carries out its operations.

    Read → check → execute
  3. Processor and output

    The hardware executes instructions; the running program displays the result.

    5 students → 6 students
This shows responsibilities, not an exact list of steps inside every language engine.

Source code has rules, not human understanding

In the first lesson’s student marks example, names represented marks and + added them. The names make the instructions easier for us to read. A language tool follows the language’s rules to determine what each name and symbol means.

The tool does not guess what we meant. If we ask it to subtract two marks, a valid program can produce a subtraction result even when the teacher wanted a total. A clear instruction can still be the wrong instruction for the task.

You only need the earlier explanation of input, processing and output to start this lesson. The code below introduces the small amount of JavaScript syntax it uses.

What the processor actually executes

The processor, also called the CPU, is the part of a computer that executes machine instructions. Such instructions can request operations such as moving a value, adding values or choosing which instruction to execute next. A useful program combines many operations rather than making one instruction say “manage a school.”

Memory holds data and instructions that a running program needs. An operating system, such as Windows or Linux, manages resources and provides services such as working with files. Language software and libraries use these facilities to help carry out a program.

Machine instructions are encoded for a particular processor architecture—a family of hardware rules. An instruction encoding for one architecture is not automatically valid for another. The same readable source can therefore lead to different low-level instructions on different machines.

Why zero and one can represent a student count

A bit is a binary digit: either 0 or 1. Hardware represents these using distinguishable physical states. A byte contains eight bits. We group bits and apply rules to represent numbers, text, pictures and other data.

Decimal numbers use place values 1, 10, 100 and so on. Binary uses 1, 2, 4, 8 and so on. In either system, a digit tells us how much of its place value to include. Read the places from right to left as they grow.

For the small number 5, three binary digits are enough: 101 means 1 × 4 + 0 × 2 + 1 × 1 = 5. It is another way to write the same count, not a different count.

Build 5 using binary places

  1. Four’s place

    The left digit is 1, so include four students.

    1 × 4 = 4
  2. Two’s place

    The middle digit is 0, so include none from this place.

    0 × 2 = 0
  3. One’s place

    The right digit is 1, so include one student.

    1 × 1 = 1
    Total: 4 + 0 + 1 = 5
These three places explain this small positive integer. They do not describe the full storage format of a JavaScript number.

Follow one more student joining the class

This program uses JavaScript. const gives a stored value a name; console.log displays a value. toString(2) produces text representing a number in base two. The 2 selects the number system, not the number of students.

Predict the four output lines, then reveal the trace. Switch samples to see a different starting count. The player uses prepared states to explain this exact source program; it does not display processor instructions or execute your own edits.

Execution workspace

Choose a sample and follow its execution. Guided simulation.

Code
JavaScript
const students = 5;
console.log(students);
console.log(students.toString(2));
const nextCount = students + 1;
console.log(nextCount);
console.log(nextCount.toString(2));
Live state

Top-level scope

students→5 (number)
nextCount→6 (number)
This step

Display the same new count as binary text: 110. This formats data; it does not show the program’s machine instructions.

Terminal
Output
5
101
6
110

What do you expect this program to print?

Binary text is data, not a compiled program

JavaScript · attendance count, starting with 5 students

const students = 5;
console.log(students);
console.log(students.toString(2));
const nextCount = students + 1;
console.log(nextCount);
console.log(nextCount.toString(2));

Expected output

5
101
6
110

The second line of output, 101, is text describing the number 5. The call to toString(2) does not translate the JavaScript program into machine instructions. It only formats a number for display.

The original students value remains 5, so adding 1 gives the number 6. Displaying a value and storing a value are different operations. The binary text also leaves out details of the actual numeric storage format.

Bits do not carry their meaning alone. The surrounding rules say whether bytes represent text, numeric data or executable instructions. Seeing zeros and ones is not enough to identify which one you are looking at. See the JavaScript documentation for numeric text conversion for this example’s language-specific operation.

Predict the count without guessing

JavaScript · change the initial attendance count

const students = 5;
console.log(students);
console.log(students.toString(2));
const nextCount = students + 1;
console.log(nextCount);
console.log(nextCount.toString(2));

Start with the code shown here. Change the first line to const students = 7;. What are the four output lines? Work out the new binary representations using place values before reading the answer.

Worked answer

The output is 7, 111, 8 and 1000, on separate lines. Seven includes 4 + 2 + 1. Eight needs the next place, 8, so its binary representation has four digits.

Only the starting data changed. The rule still adds one student. If the task were to count two new arrivals, you would also change the addition from + 1 to + 2.

What “computers understand code” does not mean

They do not read arbitrary English as program instructions. A language engine expects a particular grammar. Tools that generate code from natural language still need to produce something executable and check whether it meets the task.

Not every source program becomes a standalone machine-code file. Some tools produce an executable before you run it; others execute an intermediate representation using a running language engine. We will compare these in the lesson on compilers and interpreters.

Correct execution does not prove correct school data. A program can count the wrong list perfectly. Testing the rule and checking its input remain our responsibility. Later, different kinds of programming errors will help separate these problems.

Check: what does the binary output mean?

JavaScript · code for this question

const students = 5;
console.log(students);
console.log(students.toString(2));
const nextCount = students + 1;
console.log(nextCount);
console.log(nextCount.toString(2));

Expected output

5
101
6
110
In the displayed attendance program, what does the output 101 tell us?
Choose one answer. You can try again.

Answer and explanation

It is the starting count of 5 expressed in base two.

Yes. The place values are 4, 2 and 1, and the two 1 digits select 4 + 1. The stored starting count remains 5.

Check: where does a valid but wrong instruction fail?

A teacher wants to add two marks but writes a valid subtraction instruction. The program runs and prints the difference. Which explanation fits?
Choose one answer. You can try again.

Answer and explanation

The computer followed the written rule, but the programmer chose the wrong rule.

Yes. Executing valid instructions does not establish that they solve the intended school task.

Connect source code, execution and representation

Source code states a rule in a programming language. Language software helps carry out that rule, and hardware ultimately executes machine instructions. Memory and operating-system services support the work.

Binary is a way to represent information. For small counts, you can read it using place values. Formatting 5 as 101 shows the same number in another notation; it does not expose the compiled program.

Before moving on, explain why 7 becomes 111 and why one more student gives 1000. Then continue to how compilers and interpreters carry out source code. For more detail about the named values used here, visit variables and stored values.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Compilers and Interpreters: How Code Runs →