有什么办法来计算在编译时整数类型的宽度是多少? [英] Is there any way to compute the width of an integer type at compile-time?
问题描述
整数类型的字符
/字节是很容易计算为的sizeof(类型)$大小单位(或任何类型) C $ C>。一个常见的成语是
CHAR_BIT
乘找到的类型所占用的位数,但与填充比特的实现,这不会是等于的宽度的价值位。更糟糕的是,code,如:
X>> CHAR_BIT *的sizeof(型)-1
实际上可能有不确定的行为,如果 CHAR_BIT *的sizeof(型)
比的实际宽度键入
更大。
为了简单起见,我们假设我们的类型是无符号的。然后的宽度键入
是 CEIL(LOG2((类型)-1)
。有什么办法来计算此值作为一个常量前pression?
有一个函数宏可以确定在 *的整数类型的值的位的* ,但是只有当你已经知道类型的最大值。不管是不是你会得到一个编译时间常数取决于你的编译器,但我猜在大多数情况下,答案是肯定的。
感谢Hallvard B. Furuseth他IMAX_BITS()函数宏说,他贴在回复的在comp.lang.c 问题
/ *在inttype_MAX的位数,或以任何(1 LT;< B)-1,其中0℃= B< 3E + 10 * /
的#define IMAX_BITS(米)((M)/((M)%0x3fffffffL + 1)/ 0x3fffffffL%0x3fffffffL * 30 \\
+(米)%0x3fffffffL /((M)%31 + 1)/ 31%31 * 5 + 4-12 /((M)%31 + 3))
IMAX_BITS(INT_MAX)计算在一个int的比特数,和IMAX_BITS((unsigned_type)-1)计算在一个unsigned_type比特数。直到有人实现4 GB的整数,反正: - )
块引用>结果
和<击>信贷埃里克Sosman 击>此备用版本一>应该小于2040位工作:结果
(编辑2011年1月3日下午11:30 EST:原来,这个版本也被写了Hallvard B. Furuseth)/ *在inttype_MAX的位数,或以任何(1 LT;&LT; K)-1,其中0℃= K&LT; 2040 * /
的#define IMAX_BITS(米)((M)/((M)%255 + 1)/ 255%255 * 8 + 7-86 /((M)%255 + 12))结果
请记住,虽然无符号整型的宽度等于值的位的数量,有符号整数类型的宽度为1大于(§6.2.6.2/ 6)。这是特别重要在我原来你的问题的评论我曾错误地指出IMAX_BITS()宏计算宽度,当它真正价值计算的比特量。对于那个很抱歉!因此,例如
IMAX_BITS(INT64_MAX)
将然而,在这个例子中,我们正在处理一个符号类型创建63.编译时间常数,所以你必须加1占符号位,如果你想的int64_t的实际宽度,这当然是64的在一个单独的comp.lang.c讨论一个名为blargg用户提供了如何在宏工程细目:结果
回复:使用pre-处理器计数整数类型位。 ..请注意,该宏只适用于2 ^ n-1个值(即二进制全1)的作品,如将与任何MAX时可以预期的。同时还要注意,很容易得到一个编译时间常数无符号整数型的最大值(
IMAX_BITS((无符号型)-1)
),在写这篇文章时,我不知道任何方式做一个符号整型同样的事情,而不必调用实现定义的行为。如果我发现我会回答我自己的相关问题的SO这里:结果
<一href=\"http://stackoverflow.com/questions/4514572/c-question-off-t-and-other-signed-integer-types-minimum-and-maximum-values\">C问题:off_t(和其他符号整型)的最小值和最大值 - 堆栈溢出The size of an integer type (or any type) in units of
char
/bytes is easily computed assizeof(type)
. A common idiom is to multiply byCHAR_BIT
to find the number of bits occupied by the type, but on implementations with padding bits, this will not be equal to the width in value bits. Worse yet, code like:x>>CHAR_BIT*sizeof(type)-1
may actually have undefined behavior if
CHAR_BIT*sizeof(type)
is greater than the actual width oftype
.For simplicity, let's assume our types are unsigned. Then the width of
type
isceil(log2((type)-1)
. Is there any way to compute this value as a constant expression?解决方案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.
Credit to Hallvard B. Furuseth for his IMAX_BITS() function-like macro that he posted in reply to a question on comp.lang.c
/* Number of bits in inttype_MAX, or in any (1<<b)-1 where 0 <= b < 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))
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:-)
Andcredit to Eric Sosmanfor this alternate version that should work with less than 2040 bits:
(EDIT 1/3/2011 11:30PM EST: It turns out this version was also written by Hallvard B. Furuseth)/* Number of bits in inttype_MAX, or in any (1<<k)-1 where 0 <= k < 2040 */ #define IMAX_BITS(m) ((m)/((m)%255+1) / 255%255*8 + 7-86/((m)%255+12))
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). 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!So for example
IMAX_BITS(INT64_MAX)
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.In a separate comp.lang.c discussion a user named blargg gives a breakdown of how the macro works:
Re: using pre-processor to count bits in integer types...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 (
IMAX_BITS((unsigned type)-1)
), 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:
C question: off_t (and other signed integer types) minimum and maximum values - Stack Overflow这篇关于有什么办法来计算在编译时整数类型的宽度是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!