index

// Binary Number to Gray Code and vise versa

 

typedef unsigned int uint;

 

vector<bool> BinaryToGray(vector<bool> vBinary);

vector<bool> GrayToBinary(vector<bool> vGray);

 

vector<bool> BinaryToGray(vector<bool> vBinary)

{// note this is almost the same as below

        vector<bool> vGray(vBinary.size(), false);

 

        for(uint n = 0; n < vBinary.size(); n++)

        {

                if(n == 0)

                {

                        vGray[0] = vBinary[0];

                }

                else

                {

                        if(vBinary[n-1] != vBinary[n])  // (compare: binary - binary) (XOR)

                        {

                                vGray[n] = true;

                        }

                        //else

                        //{

                        //      vGray[n] = false;                       // already false if ==

                        //}

                }

        }

        return vGray;

}

 

vector<bool> GrayToBinary(vector<bool> vGray)

{// note this is almost the same as above

        vector<bool> vBinary(vGray.size(), false);

 

        for(uint n = 0; n < vGray.size(); n++)

        {

                if(n == 0)

                {

                        vBinary[0] = vGray[0];

                }

                else

                {

                        if(vBinary[n-1] != vGray[n])    // (compare: binary - gray)     (XOR)

                        {

                                vBinary[n] = true;

                        }

                        //else

                        //{

                        //      vBinary[n] = false;                     // already false if ==

                        //}

                }

        }

        return vBinary;

}