title: How to output the Binary value of a variable in C++
body: <p>I've got a homework assignment in my C++ programming class to write a function that outputs the binary value of a variable's value.</p>
  
  <p>So for example, if I set a value of "a" to a char I should get the binary value of "a" output.</p>
  
  <p>My C++ professor isn't the greatest in the whole world and I'm having trouble getting my code to work using the cryptic examples he gave us. Right now, my code just outputs a binary value of 11111111 no matter what I set it too (unless its NULL then I get 00000000).</p>
  
  <p>Here is my code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  #define useavalue 1
  
  using namespace std;
  
  void GiveMeTehBinary(char bin);
  
       int main(){
  
       #ifdef useavalue
       char b = 'a';
       #else
       char b = '\0';   
       #endif 
  
       GiveMeTehBinary(b);
  
       system("pause");
  
       return 0;
  
       }
  
       void GiveMeTehBinary(char bin){
  
       long s;
  
       for (int i = 0; i &lt; 8; i++){
  
           s = bin &gt;&gt; i;
  
           cout &lt;&lt; s%2;
  
           }
  
           cout &lt;&lt; endl &lt;&lt; endl;
  
  
       }
  </code></pre>
  
  <p>Thanks a ton in advance guys. You're always extremely helpful :)</p>
  
  <p>Edit: Fixed now - thanks a bunch :D The problem was that I was not storing the value from the bit shift. I've updated the code to its working state above.</p>