title: Binary To Gray Code and vice versa
body: <p>So I want to write a program that takes a binary value as input and converts it into gray code and vice versa.</p>
  
  <p>Here's what I originally wrote:</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  using namespace std;
  
  int main()
  {
  
      int Choice;
      bool g0,g1,g2,g3,b0,b1,b2,b3;
  
      cout&lt;&lt;"For Binary To Gray Code Enter 1." &lt;&lt; endl &lt;&lt;"For Gray Code to Binary Enter 2." &lt;&lt; endl;;
      cin&gt;&gt;Choice;
  
      if(Choice==1)
      {
  
          cout&lt;&lt;"Enter the Binary Bits." &lt;&lt; endl;
  
          cin&gt;&gt;b0&gt;&gt;b1&gt;&gt;b2&gt;&gt;b3;
  
  
          cout&lt;&lt;"Orginal Binary Form: "&lt;&lt;b3 &lt;&lt;b2 &lt;&lt;b1 &lt;&lt;b0 &lt;&lt; endl;
  
          g3=b3;
          g2=b3^b2;
          g1=b2^b1;
          g0=b1^b0;
  
          cout&lt;&lt;"Converted Gray Code Form: "&lt;&lt;g3 &lt;&lt;g2 &lt;&lt;g1 &lt;&lt;g0 &lt;&lt; endl;
      }
      else if(Choice==2)
      {
  
          cout&lt;&lt;"Enter The Gray Code Bits." &lt;&lt; endl;
  
          cin&gt;&gt;g0&gt;&gt;&gt;g1&gt;&gt;g2&gt;&gt;g3;
  
  
          cout&lt;&lt;"Original Gray Code Form: "&lt;&lt;g3 &lt;&lt;g2 &lt;&lt;g1 &lt;&lt;g0 &lt;&lt; endl;
  
          b3=g3;
          b2=b3^g2;
          b1=b2^g1;
          b0=b1^g0;
  
          cout&lt;&lt;"Converted Binary Form: "&lt;&lt;b3 &lt;&lt;b2 &lt;&lt;b1 &lt;&lt;b0 &lt;&lt; endl;
      }
  
      return 0;
  }
  </code></pre>
  
  <p>Now this works for 4 bits but </p>
  
  <ol>
  <li><p>I want something that determines the size of the entered binary/gray code by the user during runtime.</p>
  
  <p>I did a bit of research and found out about using vectors in such a case but since in college we just started C++ I'm not familiar with vectors or even arrays. Is there anything else that I can use to achieve it? If not, can anyone tell me how vectors can be used for it?</p></li>
  <li><p>Secondly I want to take the input in a single line without spaces.</p>
  
  <p>Example:</p>
  
  <p><code>1011</code> rather than <code>1 0 1 1</code> or taking input for each bit in a separate line.</p></li>
  <li><p>Now I also realize that I won't be able to know the no. of bits in advance so the bits formula I used to achieve XOR operation would also be changed. Is it possible to declare a binary and gray code <code>bool</code> variable and somehow perform the XOR operation on those variables rather the individual bits using simpler statements, nothing complex?</p></li>
  </ol>