title: Converting integer to base 8 using shift operator
body: <p>I have a homework problem to convert a user defined 32 bit int to binary using the shift operator. I came up with this code: </p>
  
  <pre><code>unsigned int num;
  int x = 31;
  cout &lt;&lt; "Number to be converted: ";
  cin &gt;&gt; num;
  
  while (x &gt;= 0)
  {
      int y = num &gt;&gt; x;       //using the shift operator
      if (!(y % 2))
          cout &lt;&lt; "0";
      else
          cout &lt;&lt; "1";
      x--;
  }
  
  cout &lt;&lt; endl &lt;&lt; endl;
  </code></pre>
  
  <p>This runs just fine. The second part of the homework is to elaborate and slightly modify that code to convert a user defined 32 bit int to a base 8 number, again using the shift operator. I can not for the life of me figure out how to make this work.</p>
  
  <p>EDIT: I have already tried a switch and as someone has just commented 
      <code>cout &lt;&lt; (y % 8);</code> but neither of these options give me the right answer on output.</p>