# eli5 How do people code with binary?

527 views

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

It **can** only store numbers. But we break those numbers into groups and then use those groups to lookup other stuff in a dictionary. One such dictionary called ASCII for example uses groups of 8. So 01000001 in the dictionary is A. 01000010 is B and so on.

People don’t write code directly in binary, they write it in English and software “compiles” that into binary for the computer to understand.

How did you code your question with only 26 letters?

It requires you to have a preconfigured way to interpret various sequences of the ones and zeros. And there are lots of those. A computer already knows what to do with the ones and zeros of a particular file format, and sometimes it uses the first few to tell it what to do with the rest of them. Same for ones and zeros that your phone receives from the cell tower. It gets ones and zeros that tell it which other ones and zeros are addressed to it, which in turn tell it when and where to look for other ones and zeros that are the actual data it needs.

There’s even a standard way to code for the 26 letters using strings of eight ones and zeros, so binary is at least as powerful as English for giving instructions.

[removed]

Yes, the only thing it can store is numbers, but you can represent different things with numbers. For example, the numbers will correspond to different instructions on the CPU. So if the CPU sees 100110 at the start of an instruction, that could correspond to “add these two numbers together” or “write this value to this register”. The CPU is designed to check the next few blocks of bits to figure out which two numbers we’re adding or which register we’re writing to, and then anything after that is the next instruction.

For other kinds of data, we’ve found different ways to turn numbers into other things. For example, we’ve assigned numbers to every character you can type on a computer. So an A is 65 and 😀 is 128,512. When you are writing code to deal with text, you’ll know to treat those values as text rather than numbers.

A good resource for this is Ben Eater’s youtube channel, he builds computers from scratch and you can see how all the internals work.

But for a short example: “binary” is 1 or 0, yes, but it’s really referring to on/off. For instance, the lights in your house are in “binary”, in that when they’re switched on they’re a 1, and when they’re off they’re a 0.

Now it’s possible to do more complicated things with that. We can build light switches that only turn on when two other switches are also on, or a switch that turns on when at least one of two others are on. These are called “gates”, and they do logical things—the first is an AND gate (it’s on if 1 AND 2 are), and the second is an OR gate (it’s on if 1 OR 2 are). We can also have a NOT gate, where the switch is on if one particular other switch is NOT on.

Now it turns out, we can do most calculations using these “gates”. The computer is like a gigantic house with tons of these complex switch systems, and a handful of switches that you’re allowed to touch. When we’re talking about “coding in binary”, it’s basically the same as saying “I’m going to switch some set of the lights I can touch to this particular pattern, and I want to see what other lights in the house light up”.

To make things easier over time, we’ve developed “shortcuts” that basically make it easier to understand what’s going on. For instance, we can define “1+1” to be the sequence of switches to make the computer’s “lights” light up in a way to correspond to the result of 1+1. In fact, we can make a different shortcut for + and for numbers, so that I can type into a computer “1000+230” and then that gets translated into the switches that need to be on/off to make the other lights light up in such a way that produces 1230.

As we build more and more of these shortcuts, it eventually becomes a programming language. People nowadays don’t typically code in binary—they write a programming language, which is converted in a specific way into these on/off instructions to get the computer to light up a certain way, and that “certain way” output gets translated back into things we can understand.

And again recommendation for Ben eater to get a sense of what that looks like with visuals.

Another fun way to visualize it is imagine like a pachinko board. You put marbles at the top, and they bounce to the bottom. We could build a pachinko machine that isn’t random, like say it has specific tracks where marbles put in one place are guaranteed to end up in a particular other place every time. That’s like binary code—if you put a marble in a spot it’s a 1, and if not it’s a 0. Then you let the marbles fall through the tracks, and you get some other sequence of marbles. We can assign meaning to the ending sequence because we know how the inputs produce the outputs, so it lets us do calculations and other things.

People don’t code in binary, if you mean coding as in making software.

Computers we have nowadays are based on transistors that either let electricity through or don’t. So they can be seen as being on (1) or off (0).

Data can be stored in binary by just agreeing what the data is supposed to represent. E.g. we can say that 00000001 is ‘A’ and 00000010 is ‘B’.

So it is true that nobody really codes directly in binary nowadays, but it is absolutely how things used to be done. The key thing you need to understand is that the processor is built to understand a set of instructions, and the people writing the programs know what these instructions are and how they work. Also, the instructions are fixed length. So the very basic loop of how a computer works is “load the right number of zeroes and ones from memory, look at the first so many zeroes and ones to see what my instruction in, execute it, now load the next hunk of zeroes and ones from memory.”

So for example, you the first 8 zeroes and ones might tell say “we’re going to put something into a register, which is like a little storage box that can hold a number.” The computer would then know to look at the next so many zeroes and ones to identify which register, and then the next so many for the actual number. The next instruction could then load another number into another box, and the third instruction could tell the processor to add the values in the two boxes together.

Edit: Oh and as the other comment says, letters are in fact stored as numbers. We just develop standards for, “If I tell the computer to treat this number as a letter, it will use this agreed-upon table to determine which number means which letter.” So operations that work on letters are really just operations that work on numbers with an extra step once we go to display said letters.

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.

at a high level:

> how do you get information out of that?

You define a way to interpret the 1s and 0s. For example, lets say you have the alphabet composed of 27 characters, and then some 37 other relevant characters.

The set of these 64 characters can be represented by 2^6, or 6 consecutive binary numbers.

000000

000001

000010

Given this, if we have say, 72 binary numbers we can interpret this into 12 characters by reading 6 binary numbers chunks sequentially and converting them. Now we can interpret the binary as “hello world!” for example.

Computers will have some definition of how to interpret this binary information and what to do with it.

> how do people code in binary?

In general, we dont. We use programming languages which can capture all the semantics and logic and information we want, in a format more parseable for a human being to read and write.

Then that programming language will be converted to something more useable by the computer, like for example to that binary we mentioned earlier.