The Wonderful World Of Binary
This article is part of a series, access the series index.
Programming has come such a long way and the field is so wide and varied that learning binary could be considered unnecessary. Many high-level scripting languages don’t deal in binary in any real shape or form, or so it appears to the end user. The reality of it is that binary is a conceptual tool that is used to understand one of the ‘parts’ that makes the whole computing unit (computer) operate. Thus, to start from the beginning, I believe it is important to grasp the essentials of binary so as to become more familiar with the tools that you are using.
I put ‘part’ in quotes because binary really isn’t an object or physical thing. It’s more of an organizing concept that is implemented through electrical charges. Let’s start from the beginning.
Computers Process Information
A computer is a symbolic processing machine. That means it only deals in symbols. What is a symbol? A symbol is something that represents something, like the letter A represents a sound. The word ‘dog’ represents an animal. The word animal represents a class of all living things, and so on. Computers are built to process electrical charges whose meaning are decided by us. These electrical charges must have some way to be organized to make up a symbol, which in this case we can think of as a number. This is where binary enters the picture.
There are currently only two states these electrical charges can be in: a high charge, and a low charge typically referred to as ‘on’ and ‘off’, or ‘1’ and ‘0’. Luckily, we can string these charges together to create a sequence of them such as on-on-off-on or 1101. That represents the number 13. Wait, how did I do that? How can 1101 represent 13?
This is one of the tougher concepts but we can totally get through this. A number base, to put it lay-speak, is a set of rules on how we count. Throughout our daily lives, we use the decimal system, which is known as ‘Base 10′. It’s called base ten because… well watch how you count:
1… 2… 3… 4… 5… 6… 7… 8… 9… 10…
There! Do you see how after 9 you got to 10? So what changed? The 9 turned into a 0 (zero), and an additional 1 (one) was pre-prended to the beginning. This is because… well, what else would you put past the 9? Unless you count in hexadecimal, there isn’t anything to put past 9. Think way back to your elementary days and you’ll remember why. At this point in the counting game, zero is in the one’s place and 1 is in the tens place. Each column represents a value; for example from left to right:
When one digit is in one place, you multiply that digit by that place’s value. Ex. 2 * 10 = 20 and 3 * 1 = 3 making 23.
Now you can see why it’s called “Base 10″, or decimal (as deca- means ten).
The Bi in Binary
No, it’s not a Tyra Banks tell all confessional of finding your true sexuality, it’s just a different way to count. The bi- in binary means two. It basically means you count with only two numbers: 0 (zero) and 1 (one).
What does this do to our placement values (those columns equaling powers of 10 in decimal)? Those values (1,000; 100; 10; 1) turn into powers of 2: 1, 2 = 2, 22 = 4, 222 = 8, 2222 = 16, then 32, 64, 128, etc:
It’s just a different way of counting man! Yeah yeah! So, zero is 0, one is 1, two is 10, three is 11, four is 100, five is 101.
1 (one) is the highest any particular column (placement value) can go, so as soon as we count further up it gets ‘pushed to the left’ to make room.
Binary and Electricity
Hopefully, you can see the light and have put together how the abstract concept of binary is linked, or implemented, by those on-off electrical charges in the computer.
So how would the number 700 be expressed in the computer’s ‘circuitry’?
First, you take 700 and subtract the closest binary place value: 512. That leaves us 188, and gives us a .
That means that in the end 700 is 1010111100 or on-off-on-off-on-on-on-on-off-off. Bee-de-bee-de-beep, that’s right buck!
One 1 (one) or 0 (zero) is known as a bit. For various reasons the number of bits easily employed in many computer operations are multiples of the number 8. So 8 bits is known as a byte. Oh, my god, it keeps going! 1024 bytes is known as a kilobyte, 1024 kilobytes is a megabyte, and 1024 megabytes is a gigabyte. You might hear the terms megabit, gigabit, etc. These are slightly different measurements, but in general, they are smaller than megabytes, and gigabytes.
Contrary to the title, this isn’t about the evolutionary order of carnivores, but rather a simple concept that can get somewhat confusing when you program. It’s called bit order or bit numbering. In reality, you won’t have to deal with bit numbering much these days (if at all, as it’s mostly hardware), but it’s good to know that it exists in case you do.
Bit numbering is simply a way of ordering the bits. There are two ways to number bits. They are called LSB 0, and MSB 0 [more info]. These stand for Least Significant Bit, and Most Significant Bit. A bit is ‘least significant’ when it represents the smallest amount, in this case, 0 or 1. A bit is ‘most significant’ when it represents the largest amount, such as in a byte; the 8th bit representing 128. If we call the location of the first bit (representing 1 or 0) bit number zero (the zeroth bit), then we can say that the least/most significant bits are at bit number zero. A graphic will help illustrate:
So how do we represent negative numbers? All I’ve seen is positive things around here, and while it’s looking nice, we need a little color: a little negative space to spruce things up. Well, mathematically speaking to specify a negative binary number you just say it’s negative. In computer-moon-speak however, you have to ‘tell’ the computer (give it some way to process) that a number is negative.
Simply enough, you use a bit to represent this piece of information (negativity or otherwise). It’s called a sign bit as it denotes the mathematical sign of the number (positive or negative). Often, the sign bit is the MSB [see above], halving the maximum value to be represented. Thus, an ‘unsigned’ byte could represent the numbers 0 – 255, while a ‘signed’ byte would represent the numbers -127 to 127.
However, that’s not the whole story. There are multiple ways to do this even with this one bit. There’s the sign-and-magnitude, the ones’ complement, two’s complement, and excess-n methods. There’s also a method of using a negative base (base -2) that doesn’t employ a sign bit.
How to represent negative numbers in binary has actually been relegated to its own article. So let’s move on…
Examples of Symbolic Processing
So if everything is really just bits that can be interpreted as numbers, then how does anything get done? Well, just as sequences of bits can represent numbers, they can represent other things… or in other words, the numbers can represent other things. There are two examples I can think of off the top of my head for you.
Take letters for instance, like letters on your screen. How does the computer ‘understand’ a text file? (In reality the computer doesn’t ‘understand’ anything, it just keeps track of representations for us.) The answer is what is known as ASCII (American Standard Code for Information Interchange). ASCII is an old standard that has been replaced by other text representations, but it is still in use in many places today. In ASCII letters and other types of ‘characters’ are mapped (linked) to numbers. For more information to see how this works see: http://www.asciitable.com/
How about what you see on your screen? Displays these days are so high definition it may be hard to see that the image is really made up of pixels on your screen. A pixel is one point of colored light, like on a television, whether that television is digital, analog, a CRT or a plasma. But how do we, or the computer, know what color to make the pixel? Simple, like ASCII we take a number (0 – 255) for each primary color (which in the computer world is red, green, and blue no matter what George Takei might try to tell you – but we’ll cover that in another article) and put those three numbers together to make a color. So, say 140 red, 75 green, and 90 blue would make this color. There are finer grains (granularity) of control than that, but that gives us 256 * 256 * 256 different colors alone (16,777,216).
Binary Is Your Friend
So you can see, if it wasn’t for the concept of binary, we wouldn’t be able to effectively organize the symbols to feed into our future robot overlords. Without binary, we really couldn’t represent numbers effectively or perform digital mathematical operations (a different article). So, binary, in the end as obtuse as it can be, is our friend!
I remember learning about binary when I was just an elementary student from a How Stuff Works book (as the internet was not available to general consumers; but here’s the website today: http://www.howstuffworks.com/) about computers. I must have checked out that book five times, re-reading the same material. I was fascinated with it, but pretty soon I wanted to know more. Unfortunately, my reading material was limited for several years before I could advance to higher topics. I hope this article has helped you understand a part of your computer you may not have understood before, just as the book I read way back then helped me towards achieving computer wizardry.
If you appreciate my tutorials please help support me through my Patreon.