The ten refers to two things:
There are ten digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9.
And as we go right to left in a decimal number,
each place value is ten times the place value to its right.
_ _ _ _ _ _ ^ ones place ^ tens place ^ hundreds place ^ thousands place
The two refers to two things:
There are two digits: 0, 1.
And as we go from right to left in a binary number,
each place value is two times the place value to its right.
_ _ _ _ _ _ ^ ones place ^ twos place ^ fours place ^ eights place ^ sixteens place ^ thirty-twos place
0 1 0 0 0 1 1 1 0 0 1 0 1 0 1 1 +0 +0 +0 +1 +1 +1 +0 +1 -- -- -- -- -- -- -- -- 00 01 01 01 10 10 10 11
11110 + 1101 ------
0 11110 +01101 ------
00 11110 +01101 ------ 1
000 11110 +01101 ------ 11
1000 11110 +01101 ------ 011
11000 11110 +01101 ------ 1011
11000 11110 +01101 ------ 101011
Example: Convert 01101011 Binary to Decimal.
Work from left to right. Start with ans=0. For each bit, double the ans and add the digit into the ans.
Working left to right. pattern: ans *= 2; ans += d; where d is the digit ans *= 2; ans += 0; so ans=0 ans *= 2; ans += 1; so ans=1 ans *= 2; ans += 1; so ans=3 ans *= 2; ans += 0; so ans=6 ans *= 2; ans += 1; so ans=13 ans *= 2; ans += 0; so ans=26 ans *= 2; ans += 1; so ans=53 ans *= 2; ans += 1; so ans=107 107 Decimal is the equivalent value.
Alternate approach: The left-most 1 is in the sixty-fours place, so we have: 64+32+8+2+1=107
Convert from Decimal to Binary
Example: Convert 297 Decimal to Binary.
There are two steps (both are C operations):
Remainder: num%2 is the remainder when dividing num by 2.
It is 0 if num is even and 1 if num is odd.
Quotient:
num /= 2 puts the quotient of the division into num. Suppose num is 9. Then
num /= 2 puts 4 into num.
Start with num=297.
The bits will be found from right to left.
Step 1: Compute num%2. That's a bit in the answer!
Write it down. Move to the left to be ready to write the next bit.
Step 2: Get the next num: num /=2
Repeat these two steps. Stop when num==0.
num=297 Binary = --------- r = num%2 = 1 q = (num /= 2) = 148 Binary = --------1 r = num%2 = 0 q = (num /= 2) = 74 Binary = -------01 r = num%2 = 0 q = (num /= 2) = 37 Binary = ------001 r = num%2 = 1 q = (num /= 2) = 18 Binary = -----1001 r = num%2 = 0 q = (num /= 2) = 9 Binary = ----01001 r = num%2 = 1 q = (num /= 2) = 4 Binary = ---101001 r = num%2 = 0 q = (num /= 2) = 2 Binary = --0101001 r = num%2 = 0 q = (num /= 2) = 1 Binary = -00101001 r = num%2 = 1 q = (num /= 2) = 0 Binary = 100101001