Ok, so I know that the alphabet of computers consists of only two symbols, or states: zero and one.
I also seem to understand how computers count beyond one even though they don’t have symbols for anything above one.
What I do NOT understand is how a computer knows* that a particular string of ones and zeros refers to a number, or a letter, or a pixel, or an RGB color, and all the other types of data that computers are able to render.
*EDIT: A lot of you guys hang up on the word “know”, emphasing that a computer does not know anything. Of course, I do not attribute any real awareness or understanding to a computer. I’m using the verb “know” only figuratively, folks ;).
I think that somewhere under the hood there must be a physical element–like a table, a maze, a system of levers, a punchcard, etc.–that breaks up the single, continuous stream of ones and zeros into rivulets and routes them into–for lack of a better word–different tunnels? One for letters, another for numbers, yet another for pixels, and so on?
I can’t make do with just the information that computers speak in ones and zeros because it’s like dumbing down the process human communication to the mere fact of relying on an alphabet.
In: 264
I think you are referring to stuff stored in computers memory. All the modern computers *don’t* know what they have stored in the memory, at least on hardware level. You can store texts, numbers and pieces of programs on the same stick of RAM. If you tell the CPU to read instructions from some address, it’ll happily try to do it without question.
It’s actually huge problem with modern computers. Imagine that you have some web browser that loads some webpage into memory. If attacker manages to jour program to continue from the part of memory that contains the webpage, they can execute arbitrary code on your computer. The text of the webpage would look like gibberish to the human if they saw it, but the job of CPU is to execute instructions, not question if the data in memory wasn’t intented to be displayed as text.
This just moves the question. Who knows what the data means if there is no special way to distinguish between types of the data on the hardware level? The answer is it’s job of the operating system, compiler and the programmer.
I’ve actually lied a bit about the CPU executing anything unquestionably. By default it does that, but in pretty much any case your operating system uses hardware support of your CPU to do some basic enforcement of what can be executed and what can’t.
As for distinguishing between text and numbers and pixels, it’s job of the programmer to do it. If you want, you could load two bytes that correspond to some text stored in memory and ask CPU to add them together, and it would do it as if it were two numbers. You just don’t do it on purpose, because why would you do it? Of course programmers don’t write machine code by hand they write code in some programming language and the compiler is responsible for making the machine code out of it. In most programming languages you specify what type some piece of data is. So let’s say you add two numbers in some programming language I made up. The compiler will know you are adding two numbers because you marked them as such, so when you look at the compiled machine code, it’ll probably load two numbers from memory into CPU, ADD them together and store the result somewhere in the memory. If you added two texts together, the compiler will know it needs to copy all the characters of the first text, then copy all the characters of the second text etc. It knows exactly how the characters of the text are stored and how it should know how long they are. If you try to add two things that don’t have adding implemented, you get error when compiling, so way before you run the code. So in practice, you often don’t store the type of the data anywhere, you just use it in right way.
Of course there are exceptions to everything that I said, if you want, you can store the data however you want. Interpreted programming languages store information about type of the data alongside of the data. If you are saving some data into file, you might want to put some representation of the type there too…
Latest Answers