class Integer
  
  def to_gray
    raise Math::DomainError,  if self < 0
    self ^ (self >> 1)
  end

  
  def from_gray
    raise Math::DomainError,  if self < 0
    recurse = proc do |i|
      next 0 if i == 0
      o = recurse[i >> 1] << 1
      o | (i[0] ^ o[1])
    end
    recurse[self]
  end
end

(0..31).each do |number|
  encoded = number.to_gray
  decoded = encoded.from_gray
  printf ,
         number, number, encoded, decoded, decoded
end