title: Two's complement and fmt.Printf
body: <p>So computers use Two's complement to internally represent signed integers. I.e., -5 is represented as ^5 + 1 = "1111 1011".</p>
  
  <p>However, trying to print the binary representation, e.g. the following code:</p>
  
  <pre><code>var i int8 = -5
  fmt.Printf("%b", i)
  </code></pre>
  
  <p>Outputs <code>-101</code>. Not quite what I'd expect. Is the formatting different or is it not using Two's complement after all?</p>
  
  <p>Interestingly, converting to an unsigned int results in the "correct" bit pattern:</p>
  
  <pre><code>var u uint8 = uint(i)
  fmt.Printf("%b", u)
  </code></pre>
  
  <p>Output is <code>11111011</code> - exactly the 2s complement of <code>-5</code>.</p>
  
  <p>So it seems to me the value is internally the really using Two's complement, but the formatting is printing the unsigned <code>5</code> and prepending a <code>-</code>. </p>
  
  <p>Can somebody clarify this?</p>