DEV Community

ibrahim ali
ibrahim ali

Posted on

Binary Data

As a budding software developer, I have always been curious as to what actually goes on behind the scenes of the numbers, letters, and symbols that make up the code that I write. Even more so how are the pictures rendered, and videos played, and the sound synthesized? From watching movies, we know that computer data is often represented as 1s and 0s on screen. I've learned that this is how the computer can read and interpret the code, but how exactly does it really work?

Bit
First, let's get the physical aspect of this out of the way before we delve into the logic. How exactly does the computer even tell what is 1 and what is 0? This is where a bit comes in. A bit is the smallest unit of data that a computer can hold. Hardware-wise this comes down to a single wire and whether or not it has electricity flowing through it and this state of being electrically charged or not can be effectively interpreted as some binary values; On/Off, True/False, Yes/No, and finally 1 and 0. Now one wire obviously doesn't do much for all the logic necessary to run complex computer programs, so you add more. Each new wire adding another realm of possible combinations. The Byte is the next step up and consists of an 8-bit cluster that can hold 256 different values, and these can go on exponentially to a 32-bit system having the ability to store over 4 billion values.

Positional Notation
Now that we know that we can have an incredible amount of 1 and 0 combinations with multiple bits and bytes, how do these combinations translate into data? This is achieved by translating the binary, into numbers that we can understand via positional notation. We are used to the worldwide standard of a base-ten or decimal numeric positional notation, where we have 10 unique numerical characters, 0-9, and when we've exhausted those 10 we add another character to the left and start over and this new character becomes a multiple of 10 for each additional character. The concept with binary notation is very similar except that the base-two notation goes up in 2s instead of 10s. Each additional digit indicates twice the amount of the last value. So 0 is 0, 1 is 1, but then what is 1 0? That is going to come out to 2 because the first value of zero is one and the second value is 2 and since (here the concept of off and on comes back) the 2 places has a value of 1 and the 1 place has a value of 0. You have 2 as the final value. I'm sure this sounds confusing so I'm going to include some diagrams that hopefully explain this much better.

Alt Text
Alt Text
Alt Text

Binary to Characters
Alright now that we can get numbers from binary, everything else is covered in numbers to other data and is not so much about binary anymore, but to get a better of exactly how many different 1s and 0s can go into creating the things that we see, we'll explore some of the possibilities. The first is going to be characters. ASCII (American Standard Code for Information Exchange) is the foundation of translating binary into characters and symbols. Generally, each character or symbol is represented by a 7 or 8-bit binary, which translates into the number corresponding to the character. Now the 128 characters that the 7-bit allows for encompasses most, if not all, of the basic characters we are used to using, but there are plenty of other possibilities, symbols, and languages that can require character codes that go into the thousands. This is where UTF(Unicode transformation format) becomes the new standard. UTF-8, the most commonly used UTF, uses Bytes to allow for additional character values.

Alt Text

Binary to Image
Next up we have binary to Image. This is where the possibilities with binary and then numbers get really wild. We are all aware of the primary colors(red, green, and blue) which make up all other colors perceivable by human eyes, and hopefully pixels as well, which make up the entirety of images we see electronically rendered. Computers take these numbers and perform a few calculations which let them render this. Each color has its own color code, which in turn creates all manner of combinations of RGB to render the colors we see. This combined with the numbers dictating how many pixels are present, their saturation, and how many colors in each pixel finally will render the image we see on one screen. All just wire with electrical current, screens with many tiny light bulbs, and a bunch of logic in between.

Alt Text

Binary to Sound
Lastly, we'll discuss how binary translates to sound. This I found to be the most interesting. We know that sound is vibrations, and vibrations can be visually represented as waves. The computer can pick up these vibrations via a microphone and record them at regular time intervals and place them numerically on a graph. When you connect these points to represent a wave. Now, this wave may not look perfect, but if you start taking measurements of the wave at more frequent intervals, the more points there are the more accurate the representation of the sound wave becomes and the clearer the synthesized sound with play from its capable speaker.

sources
Khan Academy and Code.org | Binary and Data
Binary Numbers and Base Systems As Fast As Possible
https://techterms.com/definition/ascii
https://techterms.com/definition/utf
https://www.bbc.co.uk/bitesize/guides/zpfdwmn/revision/2

Top comments (0)