How to convert balanced ternary number to decimal



Let's find how to convert a balanced ternary number into its decimal equivalent.

Actually, the converting formula is very simple. It derives from the binary to decimal number converter, with just one exception, that we will describe below.

First of all, we need to agree with representation of a ternary number. In balanced ternary logic, there are several common representations: "- 0 +", "-1, -0 +1", "-1, 0, 1" and "F, U, T", which corresponds respectively with Negative, Zero and Positive, or False, Undefined and True. We will use the representation "-1, 0, 1". It is one of the most popular.


Ok, let's say that we have a ternary number 0 1 0 -1 1 1 0. How to convert it into a decimal one? Like in binary conversion, we can write this number like this:

Next, we apply the following formulas: 
   - if on the top is "0" - we will not use the value from bottom
   - if on the top is "1" - we will add the bottom number
   - if on the top is "-1" - we will subtract the bottom number (here is the difference from the binary).

Simple

Let's do the math from the right to the left: 
   - "0": not used.
   - "1": add 3^1 (3)
   - "1": add 3^2 (9)
   - "-1": subtract 3^3 (-27)
   - "0": not used
   - "1": add 3^5 (243)
   - "0": not used

So, the final decimal number is: 3 + 9 - 27 + 243 = 228. Thats, it.

If we will look more detailed, we can discover an interesting fact: in balanced ternary logic, the sign of number is defined by first cipher. If it is "-1" - it means that the number will be negative, else, it will be positive. That is one of many beauties that makes difference between ternary and binary code, where for a negative number is used (and respectively lost) one bit.