title: Is there any way to compute the width of an integer type at compile-time?
body: <p>The size of an integer type (or any type) in units of <code>char</code>/bytes is easily computed as <code>sizeof(type)</code>. A common idiom is to multiply by <code>CHAR_BIT</code> to find the number of bits occupied by the type, but on implementations with padding bits, this will not be equal to the <em>width</em> in value bits. Worse yet, code like:</p>
  
  <pre><code>x&gt;&gt;CHAR_BIT*sizeof(type)-1
  </code></pre>
  
  <p>may actually have undefined behavior if <code>CHAR_BIT*sizeof(type)</code> is greater than the actual width of <code>type</code>.</p>
  
  <p>For simplicity, let's assume our types are unsigned. Then the width of <code>type</code> is <code>ceil(log2((type)-1)</code>. Is there any way to compute this value as a constant expression?</p>