Simply put a binary number is a sequence of one's and zero's.
What is a bit?
A 'bit' is a single 'binary digit' that can have one of two values, either a one '1' or a zero '0'. Best thought of as a tiny switch that can be turned on '1' or off '0'
How about a byte?
A computer 'byte' is a sequence of eight consecutive 'bits'. A single 'byte' can have a numerical value ranging from 0 - 255.
The value of a byte is determined by the value of each 'bit' within the 'byte'. Did I lose you? :P
So how is the value of a byte determined?
10010101 is equivalent to 149, but how do we know that? Let's take a look at what a 'byte' would look like in memory and how it should be imagined in our head.
Remember a 'byte' is a sequence of eight consecutive 'bits', so I want you to think of eight empty place-holders as seen below:
Each of the empty place-holders will soon be populated by our eight 'bits'. But before we focus on our 'binary number' we should know the value of each place-holder. By knowing the value of each place-holder we can determine the value of our 'byte'.
To make this easy, we always start at the far right of our empty place-holders and work our way to the left. The first place-holder always starts with the number '1'. The remaining place-holders can be determined by doubling the previous place-holder. Confused?
Alright picture eight empty place-holders. The far right place-holder is one. The next place-holder is double the previous so it is 2, then the next one is 4, and the next is 8, and we keep doubling the values as we move from right to left. So we should be seeing something like this in our head:
By knowing this information we can then begin to read 'binary numbers' in our head. So let's see why 10010101 is equivalent to 149.
In our heads it's time to fill our empty place-holders with our 'bits'. So we should be seeing something like this:
We are almost done. Now that we have imagined eight empty place-holders, determined the value of each place-holder and populated our place-holders with our 'binary number' it's time to determine the overall value.
This part is easy. Remember 'bits' are like switches, they can be turned on '1' or off '0'. So all we need to do is look at which place-holders are turned on '1' and add each of the place-holder values together.
Using the image above as a reference we can determine the value of 10010101 by doing simple addition:
128 + 16 + 4 + 1 = 149.
Now try to figure out what 11010111 is. Keep practicing!
Numbers to Binary
So you should now know how to take a 'binary number' and translate it into a decimal number (it's really that simple), but how do we do the reverse?
I've seen a lot of confusing ways demonstrated in online tutorials, but really it doesn't have to be confusing. We can use the same method we did above to figure out in our heads the binary equivalent of a decimal number.
Lets try to figure out what the number 212 is in 'binary'. The trick to accomplishing this is to imagine eight empty place-holders in your head like we did above. We then turn the place-holders on one by one until we arrive at our number. This is what we should be seeing in our head:
Starting from the far left this time (since it's easier) do it in whatever order you want. For the sake of this tutorial let's start from the left. The first thing we wanna do is look at the far-left placeholder and see if we should turn it on.
Since the far-left place-holder has a value of 128 and it is less than the number we want (212) we can turn it on which will give us a current total of 128. See below:
Lets move to next place-holder to the right. It's value is 64. Let's try turning it on and adding the place-holders together that are on. So 128 + 64 = 192. Since 192 is less than 212 we are doing good so far because we haven't gone over. This is what we are seeing in our head:
We continue this process until we arrive at our desired number (212). The next place-holder has a value of 32 so let's try turning that one on too:
Let's add the values together: 128 + 64 + 32 = 224. As you can see we have exceeded the number we were aiming for so let's go back and turn the last place-holder off in our head.
Now we are back to 192. Let's skip over to the next place-holder and see if it brings us closer to our number. Turn it on. (see below)
Now again, let's add the place-holder values together. (only the ones we've turned on). 128 + 64 + 16 = 208. So we are doing good we are closer to (212). As you can see we only need 4 more to reach (212) so we can go ahead and skip over to the place-holder that has a value of 4 and turn it on. We now have the binary equivalent to (212):
What happens if our number is bigger than 255?
A single byte can only represent a value from 0 - 255. Here's the proof:
So with all of our eight place-holders turned on '1' the maximum value we can represent in a single byte is 255.
In order to store larger numbers, computers use different data-types such as 'Intergers', 'Longs', 'Shorts', 'Floats', etc.
These data-types use multiple bytes to store greater values. Let's take a look at the 'Short' data-type. It consists of 2 bytes. Using 2 bytes we can store a greater number than we could in just one byte.
A 'data type' such as a 'short' basically takes two or more individual bytes and glues them together so the computer sees the two bytes as one single long string of 'bits'. As seen below:
Our computer interprets bytes differently depending on what type of 'data type' we are working with. As seen above the place-holder values begin at 1 on the far-right side like normal and instead of stopping at 128 and restarting our count the values continue to double until we arrive at our final bit with a value of 32,768.
Wow that is significantly larger than 255. If we turn all 16 place-holders on (1111111111111111) we would have: 32768 + 16384 + 8192 + 4096 + 2048 + 1024 + 512 + 256 + 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = 65,535.
So now by gluing 'bytes' together (data-types) we can store ridiculously large numbers. The 'long' data-type is four bytes in length and can store a number as large as 4,294,967,295. I'd be impressed if anyone can figure that one out in their heads.
Time to Recap
Try not to get overwhelmed by 'binary numbers'. They are actually quite simple in nature and if you practice translating binary numbers into decimal numbers and vice-versa using the place-holder technique described above, you'll be reading binary in your head in no time.
When dealing with larger numbers simply glue more place-holders together, determine their values and turn the bit's on until you arrive at your desired value. Remember each place-holder is double the previous place-holder.
-A bit can be 1 or 0
-A byte is 8 bits grouped together
-The value of the far-right bit starts at 1 and doubles as we move to the left
-To determine the decimal value of a byte, add the values of each bit that is set to 1 (on)
Hopefully this article has helped give you a better understanding on how to read and translate binary numbers. I will cover more advanced topics such as Bitwise Operations, Bit Masks, Hexadecimal, Signed & Unsigned values, and more in parts 2 & 3. I will also introduce some neat tricks at the bit-level.
It is important to understand and become comfortable with bits & bytes if you want to expose yourself to more advanced programming.
Cast your vote on this article 10 - Highest, 1 - Lowest
Comments: Published: 22 comments.
HackThisSite is is the collective work of the HackThisSite staff, licensed under a CC BY-NC license.
We ask that you inform us upon sharing or distributing.