title: Convert Gray Code to Decimal in R
body: <p>I have a set of 0's and 1's represented as a list initially created with <code>sample(c(0,1), n, replace=TRUE)</code>, where n is the length of my binary number. I'm currently using a BCD converter to convert my binary number to a decimal number, this is seen here:</p>
  
  <pre><code>BCD.to.Decimal &lt;- function(binaryNumb)
  {
    binaryLength = length(binaryNumb)
    decimalNumb = 0
    for(i in 1:binaryLength)
    {
      if ( binaryNumb[i] == 1)
        decimalNumb = decimalNumb + 2^(binaryLength - i)
   }
  
    decimalNumb
  }
  </code></pre>
  
  <p>I would like to instead use a GrayCode.To.Decimal converter which does the same job as my BCD.to.Decimal converter, but using Gray Code instead.</p>
  
  <p>Note: Speed DOES matter for this, and I would like to do this in the most efficient way possible. I'm aware that my BCD converter is probably not the most efficient, its just the simplest, if you have a significantly more efficient way of handling BCD conversion I'd also be interested in hearing about that.</p>
  
  <p>What is Gray Code?: <a href="http://en.wikipedia.org/wiki/Gray_code" rel="nofollow">http://en.wikipedia.org/wiki/Gray_code</a></p>