title: What is the difference between a Binary and a Bitstring in Erlang?
body: <p>In the Erlang shell, I can do the following:</p>
  
  <pre><code>A = 300.
      300
  &lt;&lt;A:32&gt;&gt;.
      &lt;&lt;0, 0, 1, 44&gt;&gt;
  </code></pre>
  
  <p>But when I try the following:</p>
  
  <pre><code>B = term_to_binary({300}).
      &lt;&lt;131,104,1,98,0,0,1,44&gt;&gt;
  &lt;&lt;B:32&gt;&gt;
      ** exception error: bad argument
  &lt;&lt;B:64&gt;&gt;
      ** exception error: bad argument
  </code></pre>
  
  <p>In the first case, I'm taking an integer and using the bitstring syntax to put it into a 32-bit field. That works as expected. In the second case, I'm using the <code>term_to_binary</code> BIF to turn the tuple into a binary, from which I attempt to unpack certain bits using the bitstring syntax. Why does the first example work, but the second example fail? It seems like they're both doing very similar things.</p>