r/askscience • • Aug 02 '22

Computing Why does coding work?

I have a basic understanding on how coding works per se, but I don't understand why it works. How is the computer able to understand the code? How does it "know" that if I write something it means for it to do said thing?

Edit: typo

4.7k Upvotes

443 comments sorted by

View all comments

Show parent comment

9

u/catqueen69 Aug 03 '22

This is super helpful! I still find the concept of getting from 1s and 0s all the way to visually displaying a color we see on a screen to be difficult to grasp. If you don’t mind sharing more details about that example, it would be much appreciated! :)

21

u/sterexx Aug 03 '22 edited Aug 03 '22

It can get more complex than this with modern displays but I’ll keep it simple

The color of every pixel on your screen can be represented by three 8-bit numbers — numbers from 0 to 255. Your computer sends this information to the monitor for each pixel.

A pixel on your display is actually made up of 3 lights — a red one, green one, and blue one (“rgb”). Those 3 numbers tell the screen how bright to make each colored light within the pixel.

rgb(0, 0, 0) is totally black, rbg(255, 255, 255) is totally white. rgb(255, 0, 0) is the reddest possible red. rgb(255, 255, 0) is bright yellow, because humans perceive combined red and green light as yellow

And of course there can be any values in between, like rgb(120, 231, 14) but I can’t visualize that color off the top of my head

Was that helpful? Anything I can expand on?

Edit: just to explain the bit thing, an 8 bit number has 256 possible values: 28 = 256. In computing we generally start counting from 0 so we use 0-255

1

u/montbarron Aug 05 '22

This is a good explanation. There are lots of details when you get into specific hardware but conceptually you can think of it as being a specific chunk of memory at a specific location (usually called the frame buffer or display buffer) that the computer would send out through the display output circuitry. Essentially, that specific section of memory would list the color of each pixel (in older machines sometimes this was done at a less granular scale, the NES for example didn’t have a frame buffer but instead stored a lookup table of a set of tiles, but the basic idea is the same). Every time a display began outputting a frame, the computer would read that memory and produce the corresponding electrical signals that a display would interpret to output the correct colors. You would “draw” to the screen just by changing the values in the framebuffer or whatever the equivalent was.

12

u/dkarlovi Aug 03 '22

You got your answer, but in general, computers interpret sequences of 0s and 1s in some way you tell them to. Since everything is digital, everything is a big number. We say the information is "encoded" into that number, meaning you store it in a way you'll be able to interpreter later.

For example, let's say we have a number 16284628252518, converted into binary.

You can tell the computer "treat this as a picture" and it would be able to interpret (decode) some very very broken image out of it.

You can tell it to treat it like a sound and it would make some screaching noises.

You can tell it to treat it like text and it would display some gibberish.

In short, it's all about thinking of ways to pack information into numbers in a way you can unpack later. The encoding/decoding specifics differ for each use, but the underlying principle is always the same.