title: How to print (using cout) a number in binary form?
body: <p>I'm following a college course about operating systems and we're learning how to convert from binary to hexadecimal, decimal to hexadecimal, etc. and today we just learned how signed/unsigned numbers are stored in memory using the two's complement (~number + 1).</p>
  <p>We have a couple of exercises to do on paper and I would like to be able to verify my answers before submitting my work to the teacher. I wrote a C++ program for the first few exercises but now I'm stuck as to how I could verify my answer with the following problem:</p>
  <pre><code>char a, b;
  
  short c;
  a = -58;
  c = -315;
  
  b = a &gt;&gt; 3;
  </code></pre>
  <p>and we need to show the binary representation <em>in memory</em> of <code>a</code>, <code>b</code> and <code>c</code>.</p>
  <p>I've done it on paper and it gives me the following results (all the binary representations in memory of the numbers after the two's complement):</p>
  <blockquote>
  <p>a = 00111010 (it's a char, so 1 byte)</p>
  <p>b = 00001000 (it's a char, so 1 byte)</p>
  <p>c = 11111110 11000101 (it's a short, so 2 bytes)</p>
  </blockquote>
  <p>Is there a way to verify my answer? Is there a standard way in C++ to show the binary representation in memory of a number, or do I have to code each step myself (calculate the two's complement and then convert to binary)? I know the latter wouldn't take so long but I'm curious as to if there is a standard way to do so.</p>