Wait, what’s with these numbers? What happened to your mission of becoming a better software engineer?
Yes, I do remember what I signed up for and it is extremely important that I deep dive into magical lands of this numbers. Still confused?
Well! Don’t you want to know about the system on which you code/write daily read and interpret this details?
Computers! I am talking about the stuff you are reading on your computer monitor right now. Do you have any idea how this information is internally represented in a computer?
I know some of you might be thinking why to bother about it. All we need to know is computer knows to perform some kind of magic. It’s like Harry Potter up in here, just remember the words to the spell and the computer will make it happen for you. Simple!
True. There’s indeed a magic in there. High-level languages shield programmers from the pain of dealing with low-level representation. Writing great code, however, requires us to understand how computers represent numbers and text. If you don’t understand it, you are just guessing how it works which could lead to inefficient, wasteful code.
This article is aimed not only at developers but any computer user who wants to learn about the magic which happens internally.
Demystifying The Magic — Numbering Systems
What is a symbol for four? 4. What is a symbol for ten? 10.
Hey, wait! Isn’t that the symbol for one and zero? Right, so in our numbering system when we get to number ten, we write the symbol for one and zero. There is no symbol for 10, we simply use the ones we have(from 0–9). This is known as decimal numbering system or (base 10).
Remember the hundreds, tens and ones place from grade school? So, if I had to represent number 123 with this, I would write one in hundred’s place, plus two times tens place plus three times ones place. Simple.
In today’s world, most people use the decimal numbering system (base 10) and most computer systems use binary representation. Confusion between the two can lead to poor coding practices.
Okay, what in the name of God is binary representation?
The binary numbering system works just like the decimal numbering system with two exceptions:
- Binary only uses the digits 0 and 1 (rather than 0–9)
- Binary uses powers of two rather than powers of ten.
Okay, let’s try to represent above number in binary form.
So, the binary representation of 123 is 01111011.
Struggling to figure out how I did this? I’ll show you two approaches for converting decimal representation to binary.
- Through an Algorithm (Yea or nay?)
- Through FingerMath
A) Through an Algorithm
(If mentioning of word algorithm makes your stomach all queasy, don’t worry, I’ve written a comprehensive guide for beginners, here)
The first step is to look at the number and if it’s even, we read 0 or if it’s odd we read 1. Then we divide the number by 2, discard the remainder or fractional component if any. Now, agahttp://jshilpa.com/the-ultimate-beginners-guide-to-analysis-of-algorithm/in check if it is even or odd and read 1/0 accordingly. Continue this till we get quotient as 0. The binary number will be the numbers read from bottom to top.
Simple, right? Let’s look at our approach number 2.
B) Finger Math:
So, like we studied before, everything in binary is about 0’s and 1’s.
Our Golden Rule :
Always remember this: Finger up denotes 1 and finger down equals 0. So, all fingers up in right-hand indicates 11111 and all fingers down indicate 00000. Cool?
Let’s Assign Some Value:
Let’s start with our right-hand, our little finger/pinkie carries a value of 1, then the next finger as 2, next finger as 4. Can you see a pattern here? We are just doubling the number each time.
So, let’s try counting now. I hope you know the golden rule by heart by now.
To count 1, we just raise our right-hand pinkie finger. Binary – 00000 00001.
To count 2, we just raise finger next to pinkie finger. Binary — 00000 00010.
To count 3, we just raise….oh wait? There is no finger marked for number 3.
But if you look closely, we have fingers marked as 2 and 1, if we raise both this fingers, their net sum would give us 3. So, to count 3 in binary, we raise two fingers marked as 2 and 1. Value of 3 in binary- 00000 00011.
Fueled by this knowledge, let’s talk about devices(read: Computer) which uses this values.
In computer theory, there is a device called “logic gates” which has two inputs, and one output.
An example of a logic gate is a “AND” gate usually implemented with a transistor. When both of the inputs are on, the output is on. Otherwise, the output is off. The only input it accepts is “on/1” or “off/0”, and the output is the same, “on/1” or “off/0”. You might see the relation to binary.
Using this simple behavior, you can build more complex logic like NAND, NOR, etc., which then builds MUXes, DEMUXes, counters, ALUs, latches, etc. With enough numbers of this little switch combined with its friends, you can create yourself a computer. You are definitely going to find my last two lines a little bit oversimplified and it’s certainly not that easy to make a computer. However the point is that logic gates, the basic building block of a computer takes 2 inputs to get an output. 2 is a very important number to a binary computer.
Don’t trust me? Listen to Master Yoda!
You might be thinking why is this article headline has numbers like 8 and 256 then?
A Quick Byte :
So, let’s talk about computer memory. Everything we read/write on the computer is stored somewhere, right?
A byte is a unit of storage in a computer which contains 8-bits and can store 256 different values: 0 to 255.
Bit(Binary digitT) is either 0 or 1. One byte is composed of 8 bits.
So, next obvious question is why eight bits in one byte? Why not two bits? Or 4 bits(also called nibble) per byte?
Maths to the rescue(again)!
Count the number of different characters we need to represent (A-Z, a-z, and 0–9, plus various other symbols such as punctuation marks, etc.).
Let’s consider we have 2 bits in one byte.
# 4 distinct combination with 2 bits in 1 byte. # 2² possible combinations of two digits which can only be either 0 or 1
2 bits - 01 10 11 11
Similarly, if there were a maximum of four bits in a byte then we could represent 2⁴ or 16 different characters.
It’s kinda obvious now we need more than 4 bits in a byte?But how many bits are actually necessary?
Early microcomputer designers originally decided that the capacity to represent 128 different characters would suffice which was later changed to 256 characters. So, to represent 256 different characters we need a maximum of 8 bits (2⁸ = 256 unique combinations).
Today our computer have giga-bytes (1 billion bytes) of disk space and megabytes (1 million bytes) of memory but 256(8 bits) still stays special as it signifies the most common base unit in a computer.
Let’s try to convert Hello World to Binary!
Wait, I hear you say how will I come to know which binary number combination represents which character?
ASCII Table To The Rescue!
This chart looks like decimal numerical representation, where is the binary code?
Yes, yes! But this article is for humans not computers right? For reading convenience for humans, the ASCII table shows the decimal equivalents for those binary numbers.
For example, “A” is represented as 01000001 inside a computer, but the ASCII table shows 65, the decimal equivalent of 01000001. We just learned in above section of our article how simple it is to convert decimal to binary, remember?
Finally, you might be thinking I have demystified the magic and know the nitty-gritty of it.
Umm, all I can say is yes, kind of only if you are an English speaker. If you are Russian or European, language specific characters are missing from ASCII table yet computer magically shows us the letters? How?
Covering this is beyond the scope of this article but there is a kickass explanation given by Joel Spolsky in this blog post.
Since Valentine Day’s is around the corner, here’s a big binary heartfelt thank you for reading this post!