2

I am trying to understand the working of gray code. If we give any non-negative integer n (where n is the number of bits) then we need to print its gray code sequence. Below are some examples

2-bit Grey Code sequence

Input = 2 bits
00 - 0
01 - 1
11 - 3
10 - 2
Output = [0,1,3,2]

3-bit Gray Code sequence

Input  = 3
000 0
001 1
011 3
010 2
110 6
111 7
101 5
100 4
Output = [0, 1, 3, 2, 6, 7, 5, 4]

According to my understanding, gray code sequence begin with 0 and in a gray code two successive values differ in only one bit. I am not sure how the gray code of 2 came [0,1,3,2] and how did the gray code of 3 came [0,1,3,2,6,7,5,4]

Paebbels
  • 15,573
  • 13
  • 70
  • 139
Coding Bad
  • 252
  • 1
  • 4
  • 12
  • you're dealing with bits - powers of two. gc(2) -> 2^2 -> 4. gc(3) -> 2^3 -> 8. since grey code has only single bit differences, the ordering of those bits sequences is NOT "linear". that's why you get that "randomish" ordering of the decimal numbers. – Marc B Jul 28 '15 at 21:06
  • Ok. So you are saying that if I give 2 then its gc becomes 2^2 = 4. So, its gc becomes [0,1,2,3] and the order could vary. Similarly in case of 3, gc becomes 2^3 = 8. I am still unsure because the https://en.wikipedia.org/wiki/Gray_code states that the gray code for 3 would be [0,1,3,2,6,7,5,4]. Please correct me if I am wrong. – Coding Bad Jul 28 '15 at 21:23
  • a 2 bit grey code can have 2^2 = 4 values. 3bit has 8 values, blah blah blah. there's nothing magical about that. that's just simple binary numbering. what makes it a grey code is that between any two positions in the list, only one bit differs. other than that it's STILL just a simple 4 or 8 or 16 or whatever number list. but the grey code is what determines the ordering of the numbers. – Marc B Jul 28 '15 at 21:25

4 Answers4

0

The usual way of generating grey code for n bits is to take the sequence for n-1 bits prefixed by 0, followed by the reversed sequence for n-1 bits prefixed by 1. Base case sequence for 1 bit is 0, 1. You can easily write a recursive function that generates this sequence:

void printgrey(int len, int pfx=0, int rev=0) {
    if (--len >= 0) {
        printgrey(len, pfx + (rev<<len), 0);
        printgrey(len, pfx + (!rev<<len), 1);
    } else
        printf("%d\n", pfx);
}
Chris Dodd
  • 119,907
  • 13
  • 134
  • 226
0

In hardware the gray encoding is described as follows:

function bin2gray(value : std_logic_vector) return std_logic_vector is
  variable result       : std_logic_vector(value'range);
begin
  result(result'left)   := value(value'left);
  for i in (result'left - 1) downto result'right loop
    result(i) := value(i) xor value(i + 1);
  end loop;
  return result;
end function; 

Source: PoC.utils

What does it mean? The highest bit (MSB) from input is copied to the result. Now, a loop traverses every input bit from MSB-1 to LSB and xors this bit with it's left neighbor.

Example:

in = 0x2 = 0010b
res(3) := 0
res(2) := in(3) xor in(2) = 0
res(1) := in(2) xor in(1) = 1
res(0) := in(1) xor in(0) = 1
return 0011
Paebbels
  • 15,573
  • 13
  • 70
  • 139
0

The easiest way to generate a gray code sequence is to start with a normal number sequence, and xor each number with itself shifted right by one bit. In Python it looks like this:

def graycodes(bits):
    return [x ^ (x >> 1) for x in range(1 << bits)]

>>> graycodes(2)
[0, 1, 3, 2]
>>> graycodes(3)
[0, 1, 3, 2, 6, 7, 5, 4]
>>> graycodes(4)
[0, 1, 3, 2, 6, 7, 5, 4, 12, 13, 15, 14, 10, 11, 9, 8]
Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
0
vector<int>vec;
vec.push_back(0);
for(int i=0;i<n;i++)
{
    for(int j=vec.size()-1;j>=0;j--)
    {
        vec.push_back(vec[j]| 1<<i); 
        // 0 ,0|1 =1, 1|2^1 = 3, 0|2^1 = 2 : a = 2 size = 1
    }
}

`

Anonymous
  • 1
  • 1