In any number system, only certain amount of symbols are used to represent data. This 'certain amount' is known as the base or the radix. For example, in Decimal Number System, we are allowed to use only {0,1,2,3,4,5,6,7,8,9}
characters. Similarly , in Binary Number System, only {0,1}
are allowed. If you want to represent any data, you can use these characters in any permutation but not any character outside this set.
Now taking your question how come 2
is represented as 10
in binary, and 3
as 11
, and so on!
We know that, there would be zero in every number system. So the counting 0,1,2,3,4,5,...(for any base) will always start with a 0
. From now onwards I will be talking about decimal and binary number systems(representing them by b10 and b2 respectively) and not of others, although if you put analogy for the other bases also, you will find that they also satisfy everything. Now since you know that there is 0
in both b10 and b2, how will you get 1
out of it?
Simply add 1
to it. That sounds rubbish for now, but lets go ahead. You now know that 1
in b10 is equal to 1
in b2. The next question is how will you get 2
when you know that 0
and 1
exist. Simply add 1
in 1
to get 2
. This works fine in b10, but what in b2 now? We can only use {0,1}
in b2 and the most significant digit is already 1
, so how to add another 1
to it?
Simply do this
1
+ 1
-----
1 0 <----- that's 2 in b2
-----
Get it little farther and you will get this
1 0 2
+ 0 1 + 1
------ ---
1 1 <------ that's 3 in b2
------
1 1 3
+ 0 1 + 1
------ ---
1 0 0 <------- that's 4 in b2
------
And so on. If you still don't get how this addition occurred, then see this. Especially 4 rules for adding 2 single digit in b2.
Decimal to Binary
Now comes the fact that what to do if b2 of bigger numbers is asked? Ofc we aren't going to add 1
that many times in binary. There is a simple way to calculate corresponding b2 of a given b10.
Given any number in b10, divide it by 2, write the remainder aside, and perform same with quotient unless quotient becomes 1. Also write that 1 aside.
For example,
see this for details. Here is a good explanation on this conversion.
I haven't talked about conversion of floating point b10 numbers, Google would be your friend in that case.
Binary to Decimal
Similarly, there is a way to calculate b10 from a given b2.
Given any number in b2, Multiply each bit with 2 raised to the power of index of that bit. The sum of all the obtained numbers is the corresponding b10 value. Index is defined as 0 and increasing by 1 leftwards from left of radix point and -1 decreasing rightwards from right of radix point.
Here is animation that illustrates this in a better way.