How does binary interface with recognizable phenomena?


I may not be phrasing this right, but basically how does the information translate from mere electronic impulse to interactive reality? How does the seemingly simple on/off nature of computer code result in such complex things as a high resolution image, audio, this website?

In: 1

> basically how does the information translate from mere electronic impulse to interactive reality?

If you had ever interacted with a taser you would not be of the impression that electric impulses are somehow not part of reality.

Electric impulses are quite real. Turning a current on or off can do things like manipulate lights, or electric motors to create movement. Images on a screen are just a bunch of tiny lights in a certain pattern, and sound is created by vibrating air using electromagnets.

High resolution images, complex audio, and things like this website are just complex combinations of these signals. How to get from on and off to that complexity is a huge topic but hopefully this addressed your core question of the transition from something you considered “unreal” to “tangible reality”.

Let’s consider a photograph.

Vision is based on light, obviously. White light can be broken into red, green, and blue components, which means that colors can be represented by describing them in terms of how much of each component they have.

A pure blue has no red, no green, and some amount of blue; how much blue determines how bright it is.

How is this related to binary, you might ask; well, since you have the ability to describe two states (off and on), you can use those two states to represent numbers.

The *decimal system* (what we’re familiar with) uses the numerals 0-9, as well as *places* — 19 has a 1 in the *tens place* and a 9 in the *ones place,* so the number is (1*10) + (9*1). As you can see, each time you move over a place, the number you’re describing goes up by a power of ten.

Similarly, the *binary system* describes numbers in powers of two. 19 in decimal can be represented in powers of two by:

1*16 + 0*8 + 0*4 + 1*2 + 1*1

or `00010011` in binary.

In this way, you can represent any number from 0 to 255 with eight **bi**nary digi**ts** — eight *bits.*

Wait. Weren’t we talking about images? Yes, we were. You can take those binary numbers and use them to represent how much of a hue there is — how much red, green, and blue light there should be.

Usually, images are represented in *hexadecimal,* another number system written in *base sixteen.* So, the brightest blue would be written #00(red) 00(green) FF(blue).

(16*15) + (1*15) = 255

With this information, a monitor knows exactly what color it should display.


The binary is useful because boolean logic–which can be expressed through binary inputs and outputs–can be used to perform computation.

But ultimately, somewhere along the line there’s a program that recognizes specific binary inputs as belonging to specific real-world phenomenon. For example, a bitmap font associates a specific binary sequence with a specific image of a letter. Abstracting away all the rest of the computation involved, when you press a key on your keyboard you’re sending a signal to the computer, which associates that signal with an image in a font file somewhere, retrieves that image, and displays it to the screen.

The image itself, high-res or not, is composed of a sequence of pixels, each of which is associated with a binary sequence in memory that the display knows how to read, and which corresponds to the color of the pixel. Higher resolution images just add more pixels per unit of space (though to be pedantic lower-resolution images have the same number of pixels, as determined by your screen, and the computer just uses one value to determine the color of a larger group of pixels on the screen).

I’m sure you can already think of this example, but let’s say you’ve got a plane cockpit, or missile launch control panel, or a car with indicator lights. If something goes wrong in the computer, there’s usually a binary representation. A bit in memory that is on, letting current pass. If you pop a light in the path of that current, you have a real world phenomenon for humans to tell what is going on inside. Your low fuel or check engine light is a great example, but I’m sure you’re looking for a bit more complex.

Quite possibly one of the most important tools in a programmer’s arsenal is the integer. A Boolean is a single bit with 2 states: on or off: current flowing ot current blocked (1 or 0 is faster to write so I’ll stick with that.) An integer is multiple bits that are read together as one number. 2 bits can be 00, 01, 10, or 11. There are 4 possible states. 3 bits have 8 states. 4 bits have 16 states… 8 bits have 256 possible states. It doubles every time.

Let’s take it very simply first and think about unsigned integers (no negative numbers). We can assign a decimal value to each possible state. That makes it a bit easier for most humans who haven’t been working extensively with binary to get an idea the relative magnitude involved. Electronics like to start counting at 0 which makes sense because it’s represented by all bits being off (a 4 bit number 0 would be 0000) up to the maximum all bits on (15 in binary is 1111). These values are not continuous, they are discrete. (You can’t have anything between 0 and 1), but they’re already much more tangible that a series of circuits that are letting electric current through.

Ex. 1: Let’s look at a few ways we can interact with that number outside the CPU that calculated it. Starting simply with a 7-segment display. If you’re not familiar, do a quick Google image search. I can almost guarantee you’ve seen these before. You can set up a series of what are called logic gates to control the outputs. An AND gate takes two signals and turns on only if both signals are on. An OR gate takes two signals and turns on if either or both signals are on. And a NOT gate takes one signal and turns on only if the signal is off. Combining these can make what are essentially if x then y statements. For instance, the number 8 lights up all 7 segments of the display and in binary looks like 1000. So we set up gates to say if A is on and B, C, and D are all off (just using letters to represent each bit in order) then turn on all the lights. This is fairly easy to do since ‘on’ means current flowing, which is what is required to turn on the lights. Now your calculator CPU can tell you what number it got when you asked it 4+4 for some reason.

Ex. 2: Next concept is called PWM which stands for pulse-width modulation. There’s a very good chance if you’ve ever held a DC electric drill with variable speed, then you’ve benefitted from PWM. I like to think of lights instead, though. Your monitor has thousands of red, green, and blue cells. Let’s pretend it’s an LED screen. That means a red LED needs to get told by the CPU how bright it should be. That number typically comes in an 8-bit integer from 0 to 255. There is, inside your computer, a clock. To a computer, a clock is very simply a signal that turns on and off very fast and repeatably.

The exact speed isn’t important, so let’s just pretend 256 pulses is 1/60th of a second. Or about 15.36 kHz. I’m sure you can imagine that computers can count and keep track of how many pulses have occured without me expressly telling you how. Let’s say the CPU turns on an output, counts 128 clock pulses, then turns off the output, waits 128 pulses, and repeats forever. Because of the way I set that up, the CPU has an output that pulses at 60 Hz, which you may know is how fast the wall voltage in North America oscillates and is fast enough that the human eye can’t detect it’s even oscillating if we were to, say, hook it up to an LED.

We’ve got our pulse, it’s on for 128 clock cycles which is called the width. Now all we have to do is modify the width. What I’ve just described is. 60% duty cycle (half the time is spent on, and half off). You can already imagine that if it’s on for half the time, it’s emitting half the photons and appears dimmer. Increasing the width increases the brightness and vice versa. Also because I’ve set up the scenario just right for the example, we can directly feed the counting circuit with our 8-bit integer to tell it how many counts to leave the pulse on, and some simple math tells us how long it spends off (256 minus the integer value). Higher number means longer pulse width means brighter pixel. 0 means no light, 255 means max brightness, on all the time.

Ex. 3: the last one I want to cover is a digital to analog converter or DAC. There are huge numbers of analog devices and phenomenon that will act the exact same, regardless of it is fed a true analog signal or an appropriate PWM signal. Some things do care. Typically either because inductance causes massive voltage spikes as the signal turns on and off rapidly, or because the information we actually want is also oscillating like audio. For those, we need to convert the integer to a proper analog signal. Most electronics run on with 5V or 3.3V, but again, for simplicity, imagine it’s 256Vour 8-bit number with 256 options is perfect for this. If the smallest bit in the integer is on, then 1V is added to the output signal. 2V for the next one, and 4V for the next. All the way up to 128V added for the last bit. That means if all 8 bits are on, the output is 1+2+4+8+16+32+64+128 = 255V. Since voltages add in series, all you need is one transistor for each bit to turn on that voltage, adding it to the signal ( well, you also need a way to generate those voltages. You can also get some advanced circuitry to smoothe out the spikes and other stuff, but in general, you have an analog signal now that you could send wherever you need it. Say, to the audio jack

Transducers. These are devices that turn one kind of physical phenomenon into another.

For instance, air pressure. Make a little metal sea!ed balloon with a flexible side. If the air outside is higher pressure than inside, the flexible part will bend a bit. Now fasten a special kind of wire on the flexible part, so it is stretched or squeezed by the flex. This wire has electrical resistance that changes depending on the stretch or squeeze. Put a constant current through the wire and measure the voltage, which will be low if the wire is low resistance, or high if high resistance. Run that voltage into a nifty circuit called an “analog to digital converter”; its output is a set of binary bits representing a number (e.g. 0011, or “3” in decimal) . Attach that ADC output to the input of your CPU, and run a program that reads that input, runs it through a math formula to map the voltage to pressure, and displays the number on your monitor: “Pressure = 1000 millibars”. Done!

Isn’t that simple?

And you can do the same thing with other physical phenomenon, like temperature, fuel flow, light intensity, ad infinitum.

For extra credit, figure out how to run this process in the other direction, so your computer can control the real world, instead of just sensing it. Hint: a circuit called “digital to analog converter” also exists.