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
Latest Answers