Microsoft Interview Question
Software Engineer / DevelopersThe reflected binary code, also known as Gray code after Frank Gray, is a binary
for example gray code for 14 is 1001 and for 15 is 1000 they differ in only one digit i.e. for successive numbers 14 and 15 gray codes 1001 and 1000 differ by just 1 digit..similar is situation for any other two successive numbers
given binary code for a number say for 2 binary code is 0010. to find its gray code. start XORing from MSB(Most Significant Bit) with 0.
0 0 1 0
|
XOR
|
0
-----------
0 <-----Result = 0
XOR this resulting 0 with next bit of binary number
Result bit XOR Binary Code's bit=
i.e. 0 XOR 0 = 0 Result 0
Then 0 XOR 1 = 1 Result 1
Then 1 XOR 0 = 1 Result 1
So gray code becomes 0011
Gray Code --- Binary Code -- decimal equivalent
- Sunaina May 29, 20070000 0000 0
0001 0001 1
0011 0010 2
0010 0011 3
0110 0100 4
0111 0101 5
0101 0110 6
0100 0111 7
1100 1000 8
1101 1001 9
1111 1010 10
1110 1011 11
1010 1100 12
1011 1101 13
1001 1110 14
1000 1111 15