Answer the question
In order to leave comments, you need to log in
How to compare two numbers in gray code?
For example we have two numbers X and Y encoded in Gray code. How can we effectively compare them?
For example:
X(10) = 12 X(2) = 1100 X(Grey) = 1010
Y(10) = 15 Y(2) = 1111 Y(Grey) = 1000
Answer the question
In order to leave comments, you need to log in
To compare two gray numbers without binary conversion, we use its reflectivity. The numbers will be compared bit by bit. If we have the first bit of X equal to 0 and Y equal to 1, then Y is greater than X. If we have two numbers that start the same way, then we still look at the first bit. Let's say we have 0 in this case, we compare the next two bits as usual (i.e. 1 is greater than 0). If we have the first bit 1, then we need to invert the comparison of the next 2nd bit (i.e. 0 is greater than 1)
For example, we have the numbers 4 (decimal) and 6 (decimal), after converting them to Gray numbers, we will have 110 and 101, respectively. As we see the first bit 1, then the next one must be compared by inverting the logic (101 is greater than 110). If the unit as the first bit is two times in a row, then we invert it two times. Let's take the numbers 8 and 9 and represent them as Gray codes 1100 and 1101. The 1st bit is the same and one - we invert the comparison logic, the 2nd bit is again one again, we invert back, the 3rd bit is the same, this is 0, then we invert nothing and compare normally the last 4th bit. In 9ke it is equal to one and in 8ke to zero. That is (1100 is less than 1101)
The meaning of the lower bits of the Gray code depends on the meaning of the higher bits, so it is hardly possible to avoid the translation of the value from the Gray code to ordinary binary. If the bitness of the code is fixed and small, most likely the best option is to convert to binary code and compare. If the bit depth is large or variable, then you can translate and simultaneously compare the already translated bits and stop at the first inequality. See _
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question