Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There is a function-like macro that can determine the ****value bits**** of an integer type, but only if you already know that type's maximum value. Whether or not you'll get a compile-time constant depends on your compiler but I would guess in most cases the answer is yes.</p> <p>Credit to Hallvard B. Furuseth for his IMAX_BITS() function-like macro that he posted in reply to a <a href="http://groups.google.com/group/comp.lang.c/tree/browse_frm/thread/35f79d1050452749" rel="nofollow noreferrer">question on comp.lang.c</a> </p> <pre><code>/* Number of bits in inttype_MAX, or in any (1&lt;&lt;b)-1 where 0 &lt;= b &lt; 3E+10 */ #define IMAX_BITS(m) ((m) /((m)%0x3fffffffL+1) /0x3fffffffL %0x3fffffffL *30 \ + (m)%0x3fffffffL /((m)%31+1)/31%31*5 + 4-12/((m)%31+3)) </code></pre> <blockquote> <p>IMAX_BITS(INT_MAX) computes the number of bits in an int, and IMAX_BITS((unsigned_type)-1) computes the number of bits in an unsigned_type. Until someone implements 4-gigabyte integers, anyway:-)</p> </blockquote> <p><br> And <strike>credit to Eric Sosman</strike> for this <a href="http://groups.google.com/group/comp.lang.c/msg/e998153ef07ff04b?dmode=source" rel="nofollow noreferrer">alternate version</a> that should work with less than 2040 bits:<br> <strong>(EDIT 1/3/2011 11:30PM EST: It turns out this version was also written by Hallvard B. Furuseth)</strong> </p> <pre><code>/* Number of bits in inttype_MAX, or in any (1&lt;&lt;k)-1 where 0 &lt;= k &lt; 2040 */ #define IMAX_BITS(m) ((m)/((m)%255+1) / 255%255*8 + 7-86/((m)%255+12)) </code></pre> <p><br> <strong>Remember that although the width of an unsigned integer type is equal to the amount of value bits, the width of a signed integer type is one greater (§6.2.6.2/6).</strong> This is of special importance as in my original comment to your question I had incorrectly stated that the IMAX_BITS() macro calculates the width, when it actually calculates the amount of value bits. Sorry about that! </p> <p>So for example <code>IMAX_BITS(INT64_MAX)</code> will create a compile-time constant of 63. However in this example we are dealing with a signed type so you must add 1 to account for the sign bit if you want the actual width of an int64_t, which is of course 64. </p> <p>In a separate comp.lang.c discussion a user named blargg gives a breakdown of how the macro works:<br> <a href="http://coding.derkeiler.com/Archive/C_CPP/comp.lang.c/2009-01/msg02242.html" rel="nofollow noreferrer">Re: using pre-processor to count bits in integer types...</a></p> <p>Note that the macro only works with 2^n-1 values (ie all 1s in binary), as would be expected with any MAX value. Also note that while it is easy to get a compile-time constant for the maximum value of an unsigned integer type (<code>IMAX_BITS((unsigned type)-1)</code>), at the time of this writing I don't know any way to do the same thing for a signed integer type without invoking implementation defined behavior. If I ever find out I'll answer my own related SO question, here:<br> <a href="https://stackoverflow.com/questions/4514572/c-question-off-t-and-other-signed-integer-types-minimum-and-maximum-values">C question: off_t (and other signed integer types) minimum and maximum values - Stack Overflow</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload