数据类型中的位数
我有两个任务要分配,一个任务返回任何机器上 int 类型的位数。我想我会像这样编写我的函数:
int CountIntBitsF() {
int x = sizeof(int) / 8;
return x;
}
看起来正确吗?
第二部分是用宏返回任意数据类型的任意位数,宏可以取自limits.h。我在我的机器上查找了limits.h,还查找了http://www .opengroup.org/onlinepubs/007908799/xsh/limits.h.html,但我认为我并不真正理解其中任何一个如何返回任何数据类型的位数。有什么想法吗?谢谢。
I have two tasks for an assignment, one return the number of bits in type int on any machine. I thought I would write my function like so:
int CountIntBitsF() {
int x = sizeof(int) / 8;
return x;
}
Does that look right?
The second part is to return the number of any bits of any data type with a macro, and the macro can be taken from limits.h. I looked up limits.h on my machine, and also http://www.opengroup.org/onlinepubs/007908799/xsh/limits.h.html, but I don't think I really understand how any of those would return the number of bits in any data type. Any thoughts? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
存储的基本单位是字符。它并不总是 8 位宽。 CHAR_BIT 在limits.h 中定义,具有字符中的位数。
The fundamental unit of storage is a char. It is not always 8 bits wide. CHAR_BIT is defined in limits.h and has the number of bits in a char.
它是
*
,而不是/
。至于第二部分,请参阅“数值限制”部分。
It's
*
, not/
.As for the second part, see the "Numerical Limits" section.
如果您想要用于在内存中存储 int 的位数,请使用 Justin 的答案,
sizeof(int)*CHAR_BIT
。如果您想知道该值中使用的位数,请使用 slebetman 的答案。尽管要获取 INT 中的位,您可能应该使用 INT_MAX 而不是 UINT_MAX。我不记得 C99 是否确实保证
int
和unsigned int
具有相同的宽度,或者只是保证它们具有相同的存储大小。我怀疑只有后者,因为在 6.2.6.2 中,我们有“如果有符号类型中有 M 个值位,无符号类型中有 N 个值位,则 M <= N”,而不是“M = N 或 M = N-1” ”。实际上,在我使用过的任何实现中,整数类型都没有填充位,因此您很可能会得到相同的答案,符号位为 +/- 1。
If you want the number of bits used to store an int in memory, use Justin's answer,
sizeof(int)*CHAR_BIT
. If you want to know the number of bits used in the value, use slebetman's answer.Although to get the bits in an INT, you should probably use INT_MAX rather than UINT_MAX. I can't remember whether C99 actually guarantees that
int
andunsigned int
are the same width, or just that they're the same storage size. I suspect only the latter, since in 6.2.6.2 we have "if there are M value bits in the signed type and N in the unsigned type, then M <= N", not "M = N or M = N-1".In practice, integral types don't have padding bits in any implementation I've used, so you most likely get the same answer for all, +/- 1 for the sign bit.
在
limits.h
中,UINT_MAX 是 unsigned int 类型对象的最大值。这意味着它是一个所有位都设置为 1 的 int。因此,计算 int 中的位数:In
limits.h
, UINT_MAX is the maximum value for an object of type unsigned int. Which means it is an int with all bits set to 1. So, counting the number of bits in an int:使用 g++ -O2,此函数计算为内联常量:
输出:
生成的 X86 32 位汇编:
---SNIP---
---SNIP---
With g++ -O2 this function evaluates to an inline constant:
outputs:
Generated X86 32-bit assember:
---SNIP---
---SNIP---
您确定需要位数,而不是字节数吗?在 C 中,对于给定类型
T
,您可以使用sizeof
运算符找到它所占用的字节数。一个字节中的位数为 CHAR_BIT,通常为 8,但也可以不同。因此,给定类型
T
,类型T
的对象中的位数为:请注意,除了
unsigned char
类型外,所有上述nbits
位的可能组合可能不代表T
类型的有效值。对于第二部分,请注意,您可以将
sizeof
运算符应用于对象和类型。换句话说,给定一个类型T
和一个该类型的对象x
:您可以通过
sizeof(T)
找到 T 的大小,以及x
的大小乘以sizeof x
。如果sizeof
用于对象,则括号是可选的。根据上述信息,您应该能够回答第二个问题。如果仍有问题,请再次询问。
Are you sure you want number of bits, not number of bytes? In C, for a given type
T
, you can find the number of bytes it takes by using thesizeof
operator. The number of bits in a byte isCHAR_BIT
, which usually is 8, but can be different.So, given a type
T
, the number of bits in an object of typeT
is:Note that, except for
unsigned char
type, all possible combinations ofnbits
bits above may not represent a valid value of typeT
.For the second part, note that you can apply
sizeof
operator to an object as well as a type. In other words, given a typeT
and an objectx
of such type:You can find the size of T by
sizeof(T)
, and the size ofx
bysizeof x
. The parentheses are optional ifsizeof
is used for an object.Given the information above, you should be able to answer your second question. Ask again if you still have issues.
您的公式不正确:您不应将
sizeof(int)
除以8
,而是应乘以int
中的字节数 (sizeof(int)
) 乘以字节中的位数,它实际上在
中定义为宏CHAR_BIT
的值。这是更正后的函数:
Your formula is incorrect: instead of dividing
sizeof(int)
by8
, you should multiply the number of bytes in anint
(sizeof(int)
) by the number of bits in a byte, which is indeed defined in<limits.h>
as the value of macroCHAR_BIT
.Here is the corrected function: