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 ...
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Code Converters - Binary to/from Gray Code
- Binary to Gray code using recursion
- Maximize the decimal equivalent by flipping only a contiguous set of 0s
- Program to convert Hexa-Decimal Number to its equivalent BCD
- Sum of decimal equivalent of all possible pairs of Binary representation of a Number
- Maximum decimal equivalent possible among all connected components of a Binary Valued Graph
- Gray to Binary and Binary to Gray conversion
- Convert Decimal To Hexa-Decimal including negative numbers
- Generate n-bit Gray Codes | Set 2
- Generate n-bit Gray Codes
- A backtracking approach to generate n bit Gray Codes
- Convert the given BCD to its equivalent Binary form
- Check whether the binary equivalent of a number ends with "001" or not
- Convert the ASCII value sentence to its equivalent string
- Check whether the binary equivalent of a number ends with given string or not
- 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
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.