-2
\$\begingroup\$

I'm not too sure if this is the correct site for this but anyway, I heard that whenever a computer program is compiled, it is turned into 0s and 1s. But how does a computer recognize 0s and 1s? A computer is electrical and electricity can't read. So how does a computer read 0s and 1s?

\$\endgroup\$
10
  • \$\begingroup\$ I'm sure this is a duplicate of something, but I can't find it.. \$\endgroup\$
    – pjc50
    Aug 4, 2016 at 23:38
  • 4
    \$\begingroup\$ Google: What is a digital circuit; Google: How a digital circuit works; Google: How to make a full adder; Google: How to make an ALU; \$\endgroup\$
    – Elbehery
    Aug 4, 2016 at 23:52
  • 1
    \$\begingroup\$ I don't see anything wrong with this question. The OP just doesn't phrase the question well. And @user2943160, it really isn't too broad, imho, since the answer to both is the same. In all cases, the 0/1 is represented as a voltage somewhere. Unless it is on a HDD in which case it is represented as a magnetic dipole which is converted into a voltage when read. \$\endgroup\$
    – jbord39
    Aug 5, 2016 at 0:23
  • 3
    \$\begingroup\$ Code: The Hidden Language of Computer Hardware and Software by Petzold \$\endgroup\$
    – old_timer
    Aug 5, 2016 at 1:00
  • 2
    \$\begingroup\$ I have two concerns with this question; 1 it shows no evidence of research or effort to understand, and 2, it is worded in such a way that it seems too broad to give a succinct, clear answer. Clearly, computers work. So " I heard that whenever a computer program is compiled, it is turned into 0s and 1s" is true. However phrases like "But how does a computer recognize 0s and 1s? A computer is electrical and electricity can't read." seem to be a 'fundamental misunderstanding' problems. Please reword to ask a simpler focused question. Please read the help center to understand how to ask good questions \$\endgroup\$
    – gbulmer
    Aug 5, 2016 at 1:17

4 Answers 4

4
\$\begingroup\$

When people say 1s and 0s, what we're really referring to are logic levels, where a 0 refers to a 'low' level and a 1 refers to a 'high' level. Since these are just voltage levels, the computer can recognize and operate on these natively.

Also, a computer program is usually stored as 0s and 1s before compiling as well, as everything on your computer will be stored that way. However, after compiling, you will get a file filled with what is called machine code. Machine code is a list of binary instructions (as opposed to text like the original source code) that can be directly interpreted by the circuitry in the processor.

Edit: adding more details as comments are too short. Let's say you have a program sitting in RAM. The RAM in your computer is called DRAM. A DRAM 'cell' holds one 'bit' of information, either a 0 or a 1. A DRAM cell consists of a single transistor and a single capacitor. The transistor can be turned on to enable access to the capacitor, either to charge it up and store a 1, discharge it and store a 0, or read the stored charge. A modern DRAM chip will have several billion of these cells, along with address demultiplexers, sense amplifiers, bus interface circuitry, auto-refresh timers, etc. The DRAM chip is connected to the processor with two wide parallel buses and some control lines. The buses are the address bus and the data bus. The address bus tells the memory chip which cells to access, and the data bus carries the data in the appropriate direction. Each bus is simply a bunch of wires in parallel, and each wire can be set to either a 'high' level or a 'low' level. These generally correspond to the power supply levels. For example, DDR2 SDRAM uses an IO standard called SSTL_18, which defines a 'zero' as 0 volts and a 'one' as 1.8 volts. Anyway, your program will be stored across a sequence of these cells in a DRAM chip. In a DRAM cell, a 1 corresponds to a cell with a charged capacitor, and a 0 corresponds to a cell with an empty capacitor. To read out the contents of the memory, the processor drives an address onto the address bus and sets the control lines to instruct the logic in the DRAM chips to read out the data at that address. The logic in the DRAM chip will connect the particular cells to the sense amplifiers and read out the charge stored in them. These levels will then be driven out of the DRAM chips on the data bus towards the CPU. The CPU will read in the voltage levels on the data bus with input buffers, adapting the voltage from the memory bus voltage to the internal CPU core voltage. Once inside the CPU core, the data will most likely be written into an SRAM based cache memory. SRAM stores each data bit in SRAM cells that generally consist of 6 transistors. Once the CPU is ready to execute the code, it will be copied out of the cache memory and onto an internal instruction bus. Logic inside the CPU will then interpret the pattern of bits on the bus and then perform the required actions to execute the instruction.

\$\endgroup\$
7
  • \$\begingroup\$ A similar question is asked here. It has a great answer by @jonk. electronics.stackexchange.com/questions/249064/… \$\endgroup\$ Aug 4, 2016 at 22:53
  • \$\begingroup\$ okay, but it's impossible to program voltage, so whenever the OS receives the request for a file to be executed, how does it send out the voltages required to run the program? \$\endgroup\$
    – Max
    Aug 4, 2016 at 23:04
  • \$\begingroup\$ The 1's and 0's that we think about are stored in the computer's memory as voltages. A "1" is usually a voltage near the system's power supply voltage, while a "0" is a voltage near ground. \$\endgroup\$ Aug 4, 2016 at 23:11
  • 2
    \$\begingroup\$ @Max We're talking about the hardware level, right? At the hardware level there is no such thing as an OS, there is only a CPU that sends out voltages because it's an electrical device. \$\endgroup\$
    – user253751
    Aug 5, 2016 at 1:10
  • 2
    \$\begingroup\$ @Max And then, once we've established that a CPU is an electrical device that sends out voltages and receives voltages, then we can talk about the CPU running programs, and figure out what "running a program" even means in terms of voltages. After that, we can define what an operating system is and how it's different from a "normal" program. \$\endgroup\$
    – user253751
    Aug 5, 2016 at 1:12
2
\$\begingroup\$

When HUMANS look at ones and zeros, we see "1" or "0" on a screen, or printed on paper, or even as a LED that is dark or lit. But what a COMPUTER "sees" as a logical zero is zero volts, and a logical one as some predetermined voltage like 5V. (Lower in modern CPUs, etc. but never zero.)

In exactly the same manner as a light switch on the wall. A HUMAN sees the light switch physically in one position or the other, but the light either "sees" the mains power and lights up, or it "sees" zero power and cannot light up.

You say "it is impossible to program a voltage". But that is not correct. That is EXACTLY what you are programming. It comes down to 0V (logic zero) or 5V (logic one).

\$\endgroup\$
0
\$\begingroup\$

The way I understand it is: A computer has components that recognize the presence of + voltage. if there is + voltage it's 1 if there isn't +voltage it's 0.

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Physical binary encoding is much more complicated than positive/zero voltage. \$\endgroup\$ Aug 5, 2016 at 0:06
  • \$\begingroup\$ 0 and 1 are very, very rarely encoded by a presence or absence of a voltage. For all practical purpose 0 and 1 are not encoded this way. 0 and 1 may be encoded by a + voltage and 0 volts, but 0V is not the absence of a voltage because it can sink current. \$\endgroup\$
    – gbulmer
    Aug 5, 2016 at 1:08
  • \$\begingroup\$ Actually, 0v is by definition the absence of voltage, because voltage is a potential difference. Don't confuse voltage and current... Digital inputs typically have a tolerance where something up to the Vil threashold will read as a zero, but the intent and what CMOS outputs come close to achieving really is quite close to the presence or absence of voltage. Of course there are also more complicated logic standards, including bipolar, inverted, and differential standards. \$\endgroup\$ Aug 5, 2016 at 2:48
0
\$\begingroup\$

Transistors can be used two ways, we know about them being able to amplify things, feed in a signal of voltage X you get "out" a signal of voltage X*Y. But that is the linear region. It can only go as high or as low as the power supply or voltage rails. So you can also use them as switches, forcing them through the linear region and slamming into either the high or low rail. Take a couple transistors, add some other elementary components and you can electrically build AND, OR, NOT, etc circuits. And from that you can wire up any kind of boolean equation you want. Now take things that oscillate, crystals or just using the nature of the speed of the electrons through a material and feedback, you can create a "one", "zero", "one"..."clock". feed the clock into the boolean equations and you can build computers. Play with feedback some more and/or take advantage of electrical components like capacitors or magnetics and you can store ones and zeros to be retrieved later. The storing and retrieving are nothing more than boolean equations built by elementary logic gates made up of transistors and other elementary components.

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.