eli5 How do people code with binary?

1.24K viewsOtherTechnology

Like I get that it can be a 1 or a 0, and that stores information, but how do you get information out of that? And how do people code with it? Seems like all it could store is numbers

Edit: Thank you all, this is very helpful

In: Technology

26 Answers

Anonymous 0 Comments

Think about how LEGO kit instructions work. They show you a picture of the part you have before, the part they want you to install, and where that part goes. That’s how, without a single word, they can show you how to build a kit even if it has thousands of pieces.

The little bits that tell the CPU what to do work like that. The CPU might argue that the binary `0000 0100 0100 1000` stands for the “LDA $48” instruction, which means “LOAD the A register into the memory address the hexadecimal number 48 represents”. In very simple terms that means “Move some information from the CPU to the memory.”

It turns out that most programs look like that if we convert the binary to CPU instructions to English. Here’s how a program might add two numbers and put the result on a console if we cut the CPU instructions and write it in English

* Load the number 2 into CPU register A.
* Add the number 3 to CPU register A and store the result in CPU register A.
* Move the value in CPU register A to memory address 1.
* Run the system code that prints a number to the screen and tell it to print the number at memory address 1, it does something like:
* Load the special number for the digit 0 into CPU register A.
* Add the number in memory address 1 to CPU register A.
* Move the value in CPU register A to memory address 2.
* Go to the program address for the system’s “print a number” code, which does something like:
* Load the memory address for “write this character to the console” into CPU register A.
* Tell the CPU to move the value in memory address 2 to the memory address in register A.
* Go back to the program address one instruction after the one that sent us here.

That’s a lot to process. That’s why people don’t tend to like writing code this way. It takes a lot of work to tell a CPU to do anything, because it can only do things one step at a time. Each “step” usually involves either moving data from the CPU to memory, moving data from memory to CPU, or doing work on things in CPU memory and putting the results somewhere else. That’s why we created “higher level” programming languages. The code above might look like this in a not-binary language:

result = 2 + 3;
Print(result);

The way we get from there to CPU instructions is very complex. A program has to look at the code and detect patterns like “2 + 3”. Then it has to convert that into “store 2 in the CPU, then tell the CPU to add 3 to it”. Then it sees the “result =” and knows that means “Move that result from the CPU to memory address 1, and remember that address is called “result”.”

That’s how we convert program code, which is just a bunch of numbers, into CPU code, which is just a bunch of different numbers.

You are viewing 1 out of 26 answers, click here to view all answers.