Gillham code is Gray code implemented as a binary-quinary combination. When a Gillham value is converted as Gray to binary in the ordinary way, the least significant 3 bits will have values of 1 through 4 and 7 ( a reflecting pattern of 5 values only).
This was done to produce a quasi decimal system. (5 X 2 =10) that would be compatible with mechanical altimeter mechanisms where one dial rotation is 1k or 10k feet.
Here is a conversion algorithm that looks quite good. I lifted it from a website, http://www.ccsinfo.com/forum/viewtopic.php?p=77544 :
signed int32 GillhamToAltitude( int16 GillhamValue )
// Data must be in following order (MSB to LSB)
// D1 D2 D4 A1 A2 A4 B1 B2 B4 C1 C2 C4
{
signed int32 Result;
int16 FiveHundreds;
int16 OneHundreds;
// Convert Gillham value using gray code to binary conversion algorithm.
// Get rid of Hundreds (lower 3 bits).
FiveHundreds = GillhamValue >> 3;
// Strip off Five Hundreds leaving lower 3 bits.
OneHundreds = GillhamValue & 0x07;
FiveHundreds = GrayToBinary(FiveHundreds);
OneHundreds = GrayToBinary(OneHundreds);
// Check for invalid codes.
if (OneHundreds 7) OneHundreds = 5;
// Correct order of OneHundreds.
if (FiveHundreds % 2) OneHundreds = 6 - OneHundreds;
// Convert to feet and apply altitude datum offset.
Result = (signed int32)((FiveHundreds * 500) + (OneHundreds *100)) - 1300;
return Result;
}
unsigned int GrayToBinary(unsigned int num)
{
unsigned int temp;
temp = num ^ (num>>8);
temp ^= (temp>>4);
temp ^= (temp>>2);
temp ^= (temp>>1);
return temp;
Chat with our AI personalities
To convert from standard binary to Gray code you need to be familiar with the logical XOR operator (eXclusive OR). The XOR operator outputs a 1 bit if either of two input bits is 1, but not both. The truth table for all possible inputs p and q is as follows:
p q output
0 0 0
0 1 1
1 0 1
1 1 0
Armed with this knowledge, we can convert from standard binary code to Gray code using the following algorithm:
Step 1: Fix the most-significant bit, MSB, which is always the same for both. If there is only one bit, then we're done. Otherwise proceed to step 2.
Step 2: XOR the first two bits of the binary code to get the next Gray bit.
Step 3: Discard the first bit of the binary code.
Step 4: If there are two or more bits left in the binary code, goto step 2, otherwise we're done.
Example: Binary = 11001010
(Fix MSB) = 11001010
(1 XOR 1 = 0) 001010
(1 XOR 0 = 1) 01010
(0 XOR 0 = 0) 1010
(0 XOR 1 = 1) 010
(1 XOR 0 = 1) 10
(0 XOR 1 = 1) 0
(1 XOR 0 = 1)
Thus: Gray = 10101111
Note that we XOR pairs of bits in the binary code from left to right until there are no pairs left. The bold bits are the fixed Gray bits reading from top to bottom.
To reverse the process, we need another algorithm, but we still use XOR:
Step 1: Fix the most-significant bit, MSB, which is always the same for both. If there is only one bit, then we're done. Otherwise proceed to step 2.
Step 2: XOR the binary bit we just set with the next Gray bit to fix the next binary bit.
Step 3: If there are more Gray bits, repeat step 2, otherwise we're done.
Example: Gray = 10101111
(Fix MSB) = 10101111
1 XOR 0 = 1
1 XOR 1 = 0
0 XOR 0 = 0
0 XOR 1 = 1
1 XOR 1 = 0
0 XOR 1 = 1
1 XOR 1 = 0
Thus: Binary= 11001010
Note that we carry the fixed bit (in bold) from the previous line into the next line and XOR with the next Gray bit to get the next fixed bit. The bold bits are the fixed binary bits read from top to bottom. We could also write this out as follows, reading left to right:
Gray = 10101111
Binary = 1 XOR 0 = 1 XOR 1 = 0 XOR 0 = 0 XOR 1 = 1 XOR 1 = 0 XOR 1 = 1 XOR 1 = 0
Previous Answer:
[Edit: The previous answer uses arithmetic ADD rather than logical XOR. Apart from anything else, it makes no logical sense for 1 ADD 1 = 0 (it is actually 10 binary, or decimal 2). But so long as you only consider the LSB (least-significant bit) the result will be the same. However 1 XOR 1 is always 0 and is therefore more efficient.]
How do you convert binary to Gray code?
Very Simple Example: Convert natural binary 11001010 to gray code.
Step 1) Copy most significant bit (on very left)
Answer: 1
Step 2) Add from left to right bit by bit of your binary number:
(1+1) 001010 [1+1 = 0]
Answer: 10
Step 3) Continue:
1(1+0)01010
Answer: 101
-------------------------
11(0+0)1010
Answer: 1010
------------------------
110(0+1)010
Answer: 10101
-------------------------
until you get to the end
110010(1+0)
Answer: 10101111 <- Answer in Gray Code
To convert from Gray code to binary code, proceed as follows:
Step 1: Fix the most-significant bit (always the same for both).
Step 2: XOR the most recent binary bit with the next bit in the Gray. Fix the result as the next binary bit.
Step 3. Repeat step 2 until all digits are fixed.
Examples (bold indicates a fixed binary bit):
Gray: 0111 = 0 XOR 1 = 1 XOR 1 = 0 XOR 1 = 1
Binary: 0101 (5 decimal)
Gray: 1011 = 1 XOR 0 = 1 XOR 1 = 0 XOR 1 = 1
Binary: 1101 (13 decimal)
Gray: 1101 = 1 XOR 1 = 0 XOR 0 = 0 XOR 1 = 1
Binary: 1001 (9 decimal)
To convert from binary code to Gray code, proceed as follows:
Step 1: Fix the most-significant bit (always the same for both):
Step 2: XOR the first two binary bits. Fix the result as the next Gray bit.
Step 3. Discard the first binary bit. If there are two or more binary bits remaining, goto step 2.
Examples (bold indicates a fixed Gray bit):
Binary: 0101 = 0, (0 XOR 1) = 1, (1 XOR 0) = 1, (0 XOR 1) = 1
Gray: 0111 (decimal 5)
Binary: 1101 = 1, (1 XOR 1) = 0, (1 XOR 0) = 1, (0 XOR 1) = 1
Gray: 1011 (decimal 13)
Binary: 1001 = 1, (1 XOR 0) = 1, (0 XOR 0) = 0, (0 XOR 1) = 1
Gray: 1101 (decimal 9)
[EDIT: The previous answer was wrong as it incorrectly assumed converting both ways was achieved with the same algorithm. It is not. Moreover, the algorithm that was used (Binary to Gray) used an arithmetic ADD instead of a logical XOR. Although the end result is essentially the same, ADDing two set bits forces a redundant carry bit, whereas XOR does not. Thus XOR is more efficient.]
1.write the the MSB bit as the MSB of the binary.
2.if the 2nd grey bit is 0,then the 2nd binary bit is the same as the first binary bit.... and if the grey bit is 1 the 2nd binary bit is the complement of the first binary bit
3.step 2 is repeated for every bit....
EXAMPLE:the binary of the grey code 1110011 is 1011101
Its often done in hardware with a lookup ROM, or in software with a lookup table.
gray code is one which changes one bit at a time but binary code is one which changes one or more bit at a time. for example three bit binary and gray code the left one is binary and the right one is gray code.binary gray000 000001 001010 011011 010100 110101 111110 101111 100000 000
5
The gray code for the decimal number 6 in four-bit format is 1011. To convert from binary to gray code, the most significant bit (MSB) remains the same, and each subsequent bit is derived by XORing the current bit with the previous bit in the binary representation. The binary representation of 6 is 0110, which converts to gray code as follows: 0 (MSB), 1 (0 XOR 1), 1 (1 XOR 1), 1 (1 XOR 0), resulting in 1011.
BCD codes,gray code,error detecting code,ASCII character code,Excess 3 code
Gray code is a 'reflected code', why is it named so will be illustrated soon. The advantage of Gray code over binary code is that only one bit in the code group changes when going from one number to the next. By, Ashish Kumar (Roh, Nawada, Bihar)