Hi, I am working on a program that uses an array of integers(1 and 0) to represent a gray code. For this to work I first have to take an integer and convert it to the binary, then convert the binary to gray code. The for loops i use to do this are shown below.

//Calculate Next State
     for(i = 0; i < SIZE; i ++){   //Decimal to binary conversion
          graycode_I16[SIZE-1-i] = (int)((my_timer_UI32 >> i) & 1);

    for(i = SIZE-1; i > 0; i --){ //binary to gray code
        graycode_I16[i] = graycode_I16[i] ^ graycode_I16[i - 1];

My problem with this is that this graycode holds time in milliseconds so inorder to increment this I have to increment the decimal time in ms, then convert to binary, then convert to graycode again and this is taking far more time than I would like. So my question is, would it be possible to increment graycode_I16 to so that it would increment the ms by 1? Hopefully this is detailed enough, if you need more details let me know.

4 Years
Discussion Span
Last Post by Banfa

You can increment using something like this assuming a grey code number with n bits numbered from LSB 0 to MSB n-1

Get the count of the number of bits set to 1

if count is even
    invert bit 0
   set j = the number of the least significant bit set to 1
   if (j == n-1)
       invert bit j
       invert bit j+1

I kind of worked this out emperically rather than finding a proof for it so you may wish to verify it over a wide range of numbers.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.