1. Prologue
I recently read a wonderful article
[1] . In it, the author tells that computers were not always binary. At the dawn of the computer age, there were machines that used the decimal and ternary number systems.
The decimal system is convenient to man, but it is rather difficult to implement on the existing element base. In addition, the decimal system is prone to errors due to signal distortion during transmission. The ternary system is not much harder to implement the binary (
[2] ), but it can provide at least three advantages.
First, the representation of negative numbers becomes natural. Since the minimum unit of information in the ternary system (we will call it trit) is able to take three possible values, it becomes possible to represent zero and a positive number in different ways.
Secondly, a third (a set of eight trit) is able to take values ​​in the range of ± 3,280, including zero, which is approximately thirteen times the number of possible byte values ​​in the range of only positive numbers (bytes cannot take negative values ​​in principle without introducing special conventions ).
Thirdly, it is capable of accepting the value “not determined”, which is closer to the model of human thinking than the totality of binary values ​​“true” and “false”.
The purpose of the article is to propose a method for implementing basic logical operations with ternary values ​​using a binary computing system.
2. Trinary-binary transformation
The first thing we need to do is find a method for representing ternary numbers in a binary computing system. As already mentioned, trit is able to take three logical values: "false", "undefined" and "true." Three possible values ​​can be represented by two bits, but unlike the binary system, the bits here are unequal.
Let the first bit determine the logical values ​​of “true” and “false”. The second bit (one-and-a-half) is the manager. The definiteness of the value of the trit depends on its value. If half a litter is set - the value of the trita is determined, if cleared - not defined.
Thus, two bytes are needed to represent a tayt. The simplest implementation seems to be to “assemble” control half-words into one byte, and all “meaningful” half-words into another.
The total value of trit is determined by the following algorithm: the value of the half-rubrite from the control byte is analyzed. If it is false, the value of trit becomes “undefined”; otherwise, the value of trit becomes the value of the meaningful semitrit.
Example:Let trit in waste can be one of the following values:
1 - “truth”
-1 - "false"
0 - "not defined"
')
Significant byte 0010 1100
Control byte 1100 1010
Trite -1-100 10-10
In a binary system, a treyt can be represented as follows:
01011000 11100100
Thus, binary analogs of ternary values ​​will have:
1t = 10b ~ «»
0t = 00b ~ « »
0t = 10b ~ « »
-1t = 01b ~ «»
Note: As you can see, the value “undefined” can be represented in two ways. This is due to the redundancy of possible values ​​of two bits to represent one trit.
3. The operations of the ternary logic
There are five basic operations in binary logic that cannot be expressed through each other:
1. Negation ("NOT", ~);
2. Repetition ("REP");
3. Conjunction ("AND", &);
4. Disjunction (“OR”, |);
5. Shift ("<<" and ">>").
In parentheses are the designations of operations adopted in the C programming language (except for paragraph 2, since there is no repetition operation in C -
author's comment ).
In the second part, it was said that the analysis of the value of trit begins with the analysis of the value of half-litter. If its value is “undefined”, it does not make sense to analyze the second half-litter - the value of the whole trit becomes “undefined”. If a trit value is defined, logical operations apply to trites in the same way as bits.
Example:-1011 -1-1-10 & 000-1 11-10
Imagine binary priits:
-1011 -1-1-10 = 01001111 01010100
000-1 11-10 = 00000001 11110100
We divide the priyt into a control and a significant byte (ST is a significant byte, UB is a control byte):
01001111 01010100 = 0011 0000 () 0011 1110 ()
00000001 11110100 = 0000 1100 () 0001 1110 ()
Perform the operation as with binary bytes:
0000 0000 () 0001 1110 () = 00000001 01010100 = 000-1 -1-1-10
The shift is carried out in a similar way, only it should be taken into account that the shift operation is performed on the trit, which in the binary system is denoted by two bits. In other words:
-1011 -1-1-10 >> 2 = 01001111 01010100 >> 4 = 00000100 11110101 = 00-10 11-1-1
4. Trinity arithmetic
The laws of ternary arithmetic are similar to the laws of mathematics for the ternary number system. For the convenience of presenting numbers it is worth accepting
0t = 00b
1t = 01b
2t = 10b
Thus, the algebra of logic is very different from computational algebra. To indicate the sign of a number, half a litter with the value “11” will be used in the same way as it happens in the binary representation of a negative number.
Note: At the beginning of the article I intend to inaccurate. Now it is obvious that the value of one benefit lies in the range of ± 1,143, which is nevertheless almost 9 times larger than the range of ± 128.
4. Conclusion
In the article I tried briefly and, if possible, to present my vision of the implementation of ternary calculations on binary systems. Of course, without hardware support, ternary logic and arithmetic can become more interesting fun than find serious use.
Systems with a very different architecture from Intel exist now. Perhaps sometime in order to reduce costs and increase productivity, the giants of the global digital industry will turn to the developments of the 50s: the idea of ​​an automatic calculator was born many centuries before the appearance of a tear-off computer.
PS The post expresses the point of view of the author and in no case claims the effectiveness of the realization or the title of truth.