Sunday, November 2, 2014

Number Systems

Before we delve further into designing digital systems, I think it is important to know a little more about another very basic concept - that of different number systems used in the digital world. All our design methods later on will depend on these number systems, especially one in particular - the binary system.

Let's start with the number system we are most familiar with. It is called the decimal system, since it is based on powers of ten. Why did the decimal system become so popular amongst us humans? Some say that is because we have ten digits on our hands, and hence prefer to count in multiples of ten. Well, that is at least how the decimal system started. So, let's take a deeper look into the system. The system is based on 10 numbers, or "digits", {0, 1, 2, 3, 4, 5, 6, 7, 8, 9} = {0 to (10-1)}. Any number you want to express in this system, can be written as a combination of these ten digits and a power of 10. Eg.,
\(\begin{equation} 173=1*10^{2}+7*10^{1}+3*10^{0} \end{equation}\)
And so can any other number be decomposed. Here, the number 10 is called the "base" or "radix" (Latin for "root") of the number system.

Let us now take any general number system, with a radix, say, N, where N is an integer. Such a number system will consist of N digits, viz., \( \{0, 1, 2, ... , N-1\} \). (In case of decimal systems, N = 10, and so the digits are \( \{0, 1, 2, ... , 9\} \)). Any number in that system can be expressed as,
\(\begin{equation} (xyz)_{N}=x*N^{2}+y*N^{1}+z*N^{0} \end{equation}\)
where \( x,y,z\in\{1,2,...,N-1\} \). The right side of the above equation evaluates to the value of the number in the decimal system. The left hand side is the value of the number in the base-N numbering system, as is denoted by the subscript_N.

Now equipped with these terms, we are beginning to get an idea of what these number systems are all about. Well, from a bird's eye view, they appeared really complicated, didn't they? I mean, what are binary, ternary, hexadecimal systems all about! Even their names are so complicated! But now that we know how these systems work, they don't seem so formidable after all.

So, how does the binary system work? Binary means the system has only two digits, \( \{0,1\} \). Hence its radix is? That's right, 2. So, say I give you a binary number \( (11001011)_{2} \), what is the number in decimal?
$$ (11001011)_{2}=1*2^{0}+1*2^{1}+0*2^{2}+1*2^{3}+0*2^{4}+0*2^{5}+1*2^{6}+1*2^{7} $$
$$=1+2+0+8+0+0+64+128=(203)_{10} $$

Till now we have understood what the number systems mean and what any number in any number system is in the decimal system. Now, let us divert our attention to another important aspect - how to convert from the decimal number system to any other number system. Since we multiplied each digit of the number by its radix_to_some_power and added them all up to get the decimal number, it seems logical that while converting back to radix_N, we should divide the decimal number by the radix and take the remainder at each stage. Let us try and see this more mathematically. As we saw in the previous example,
$$ (203)_{10}=1+2+0+8+0+0+64+128 $$
$$ =1*2^{0}+1*2^{1}+0*2^{2}+1*2^{3}+0*2^{4}+0*2^{5}+1*2^{6}+1*2^{7} $$
$$ \Longrightarrow(203)_{10}/2=\frac{1}{2}(1*2^{0}+1*2^{1}+0*2^{2}+1*2^{3}+0*2^{4}+0*2^{5}+1*2^{6}+1*2^{7}) $$
which leaves a remainder $ 1 $ and a quotient $ 1*2^{0}+0*2^{1}+1*2^{2}+0*2^{3}+0*2^{4}+1*2^{5}+1*2^{6} $. Thus, we get the LSB (Least Significant Bit) or the right-most digit of the binary number. If we continue this process with the quotient obtained, we'll get the next right-most digit, and so on, until the quotient becomes 0 and we get the MSB (Most Significant Bit) as the remainder.

That's a pretty easy procedure to follow! Just be very careful that you get back the digits in the reverse order. But some of you may wonder why do we have to remember two different processes from converting from decimal to base N and vice-versa. Shouldn't there be a single process that covers both? Turns out, you can apply a similar process, only complication being that the numbers now have to be all represented in radix N. Let's see in the next example what this means:
$$ (203)_{10} = 2*10^{2}+0*10^{1}+3*10^{0} $$
$$ = (10)_{2}*(1010)_{2}^{2}+0*(1010)_{2}^{1}+(11)_{2}*(1010)_{2}^{0} $$
where we have represented all decimal numbers as their binary equivalents. Thus $2_{10}=10_{2}$, $3_{10}=11_{2}$ and $10_{10}=1010_{2}$. As you can see, this makes the whole process very complicated and defeats the entire purpose, as now you are supposed to know the binary values of all these decimal numbers, and then you are also supposed to be good at binary multiplications! And even if you manage to do that, imagine what happens when we change the radix! 

The only purpose this entire procedure serves is that now we know how to convert from any base to any other base; not just to and from base 10. Say we want to convert the number $122_{3}$ to its binary equivalent. We can straightaway proceed as follows:
$$ (122)_{3}=1*3^{2}+2*3^{1}+2*3^{0} $$
$$ =1*(11)_{2}^{2}+(10)_{2}*(11)_{2}^{1}+(10)_{2}*(11)_{2}^{0} $$
$$ =1*(11)_{2}*(11)_{2}+(110)_{2}+(10)_{2} $$
$$ =(1001)_{2}+(110)_{2}+(10)_{2} $$
$$ =(10001)_{2} $$
The result can be verified readily by converting each of the representations to the corresponding decimal value. This I am leaving as an exercise you can try yourselves.

We have thus seen some of the bases in which we can represent numbers - binary (base 2), ternary (base 3), and decimal (base 10). Some of the other important radices (plural of radix) are 8 (octal number system) and 16 (hexadecimal number system). These systems are used extensively in computer programming and digital system design since they can represent binary numbers in more compressed formats (please note that 8 and 16 are powers of two after all - can you explain why this fact helps?). One thing to note in hexadecimal systems is how the digits are represented (sadly we have only 10 digits due to the shortage of fingers (digits) in our hands (the ancients should have considered the 10 toes as well when counting!)). The hexadecimal digits are $ \{0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F\} $. The last six digits are represented (sadly, in need of a better choice) as the first 6 alphabets in the English language.

So, start playing around with the number systems. Till the next time.