Converting a number from base3 to base2 without going through base 10.

85 Views Asked by At

Could anyone guide me on how to directly convert a number from base 3 to base 2 without using base 10? For example, converting "2101" (base 3) directly to base 2. Any suggestions, method or algorithms would be greatly appreciated.

Thanks in advance!

1

There are 1 best solutions below

0
On BEST ANSWER

There isn't really any issue with converting directly, exactly the way you would to base 10, only do it to base 2 instead. The biggest hurdle is probably to do all the base 2 arithmetic, because it's not something that people practice as much as they practice base 10 arithmetic.

In base 3, we have $$ 2101 = 2\cdot 10^3 + 1\cdot 10^2 + 0\cdot10^1 + 1\cdot 10^1 $$ Converting all of these numbers into base 2, we get: $$ 10\cdot 11^3 + 1\cdot 11^2 + 0\cdot 11^1 + 1\cdot 11^0\\ = 10\cdot 11011 + 1\cdot 1001 + 0\cdot 11 + 1\cdot 1\\ = 110110 + 1001 + 0 + 1\\ = 100\,0000 $$ (Note that I didn't bother to touch the exponents. They can stay in base 10 in my opinion.)


Instead of adopting a "base-three-to-base-ten" algorithm to suit your needs, we could adopt a "base-ten-to-base-two" algorithm. In this case repeated division by $2$, where all the remainders make up the representation we're after, in order from lowest significance to highest. The arithmetic involved here is base 3 division rather than base 2 multiplication:

  • We have $2101\div 2 = 1012$, with remainder $0$, so the one-bit in base two is $0$
  • We have $1012\div 2 = 121$, with remainder $0$, so the two-bit in base two is $0$
  • We have $121\div 2 = 22$, with remainder $0$, so the four-bit in base two is $0$
  • We have $22\div 2 = 11$, with remainder $0$, so the eight-bit in base two is $0$
  • We have $11\div 2 = 2$, with remainder $0$, so the sixteen-bit in base two is $0$
  • We have $2\div 2 = 1$, with remainder $0$, so the thirty-two-bit in base two is $0$
  • We have $1\div 2 = 0$, with remainder $1$, so the sixty-four-bit in base 2 is $1$
  • We have reached $0$, so we are done

All in all we reach $100\,0000$.