Given a decimal number n. Find the gray code of this number in decimal form.
Input : 7
Output : 4
Explanation: 7 is represented as 111 in binary form. The gray code
of 111 is 100 in binary form whose decimal equivalent is 4
Input : 10
Output : 15
Explanation: 10 is represented as 1010 in binary form. The gray code
of 1010 is 1111 in binary form whose decimal equivalent is 15
Following table shows the conversion of binary code values to gray code values:
|Decimal Value||Binary Equivalent||Gray Code Equivalent||Decimal Value of Gray Code Equivalent|
Below is the approach for the conversion of decimal code values to gray code values.
Let G(n) be Gray code equivalent of binary represention n. Consider bits of a number n and a number bit G(n). Note that leftmost set bits of both n and G(n) are at same position. Let this position be i and positions on right of it be (i+1), (i+2_, etc. The (i + 1)th bit in G(n) is 0 if i-th bit in n is 1 and vice versa is also true. The same is true for (i+2)-th bits, etc. Thus we have G (n) = n xor (n >> 1):
Finding the Gray Inverse Code
Given the decimal equivalent number n of a gray code. Find the inverse number in decimal form.
Input : 4
Output : 7
Input : 15
Output : 10
Below is the approach for the conversion of gray code values to decimal code values.
We will go from the older bits to the younger ones (even the smallest bit has number 1, and the oldest bit is numbered k). We obtain such relations between the bits of the ni number n and the bits of the gi number g:
nk = gk, nk-1 = gk-1 xor nk = gk xor gk-1 nk-2 = gk-2 xor nk-1 = gk xor gk-1 xor gk-2 nk-3 = gk-3 xor nk-2 = gk xor gk-1 xor gk-2 xor gk-3 ...
- Digital logic | Code Converters - Binary to/from Gray Code
- Binary to Gray code using recursion
- Sum of decimal equivalent of all possible pairs of Binary representation of a Number
- Gray to Binary and Binary to Gray conversion
- Convert the ASCII value sentence to its equivalent string
- Generate n-bit Gray Codes
- Generate n-bit Gray Codes | Set 2
- A backtracking approach to generate n bit Gray Codes
- Modular multiplicative inverse from 1 to n
- Adjoint and Inverse of a Matrix
- Modular multiplicative inverse
- Fast inverse square root
- Finding inverse of a matrix using Gauss - Jordan Method | Set 2
- Program to implement Inverse Interpolation using Lagrange Formula
- Chinese Remainder Theorem | Set 2 (Inverse Modulo based Implementation)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.