eli5 How do people code with binary?

1.26K 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

The processor in your computer has a bunch of “instructions”, things it knows how to do, like “move what is in one memory cell to a register”, or “add what is in these two registers and put the result back into the first register”.
So for instance, to add two numbers a program would do something like:
Move memory cell number 3 to register 0.
Move memory cell number 4 to register 1.
Add register 1 to register 0.
Move register 0 to memory cell number 5.

Programmers who write code at the processor level use a language called assembly, which looks a lot like that example, but it would be something closer to:
“`
MOV 03 R0
MOV 04 R1
ADD R1 R0
MOV R0 05
“`

This is an imaginary version of assembly for an imaginary CPU, but real assembly looks a bit like that. The name of an instruction, followed by the “arguments”, information needed for the instruction to do its job.
This is very close to what is actually sent to the CPU, except the CPU doesn’t understand text, it understands binary.
Let’s say that on our imaginary CPU, the MOV instruction is 0100, and add is 1101. Let’s also imagine that memory cells are binary numbers that start with 0, so 0000 to 0111, and registers and other special memory starts with 1, so 1000 to 1111.
This would be a 4-bit CPU.

Let’s go ahead and translate our program to binary, ready to send to the CPU:
“`
0100 0011 1000
0100 0100 1001
1101 1001 1000
0100 1000 0101
“`

I typed it out as 4bit chunks, with one instruction on each line, but the CPU would receive something more like `010000111000010001001001110110011000010010000101`
The CPU would decode that by chopping the input into 12bit chunks (since each instruction is made of three 4bit codes), and then read the 4bit instruction name and the two 4bit arguments.

Note that real processors use much bigger chunks. Most modern CPUs use 64bit chunks, which is what it means for a processor to be a 64bit CPU or old consoles to be 16bit consoles.
This is also an extremely simplified explanation, just to show how a computer “understands” binary code.

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