查找整数位数 [英] Finding number of bits of integer
问题描述
我需要在C中实现类似于C ++的std :: bitset的东西;对于
这个,我使用一个int'的数组来组合任意数量的
位,可能大于32/64或类似的东西。 />
当然,单个int有多少位并不重要,但我确实需要一个可靠的方法来找到它。
我想起像
CHAR_BITS * sizeof(int)
就可以了,我就在这里吗?
我只是觉得它是* CHAR * _BITS;参考通常的
示例,有些机器中char'有9位 - 但是在
这种情况下int要求有9的倍数比特呢?即,
sizeof(某物)总是给它的大小为
sizeof(char)的倍数,或者这样的9位字符可以与16/32位配对整数?
非常感谢,
Daniel
-
得到两个亲爱的丹尼尔即时消息
由MSN,ICQ与压力联系 - 所以
请使用好的旧电子邮件!
Daniel Kraft说:
我做需要在C中实现类似于C ++的std :: bitset的东西;对于
这个,我使用一个int'的数组来组合任意数量的
位,可能大于32/64或类似的东西。 />
当然,单个int有多少位并不重要,但我确实需要一个可靠的方法来找到它。
我想像
CHAR_BITS * sizeof(int)
就可以了,我就在这里吗?
嗯,你的意思是CHAR_BIT,但是是的,这将给你int占用的总数
位 - 包括符号bit,至少(但可能是
以上)15个值位,至少(但可能更多)没有填充
位。
我只是觉得它是* CHAR * _BITS;参考通常的
示例,有些机器中char'有9位 - 但是在
这种情况下int要求有9的倍数比特呢?
是的,CHAR_BIT给出一个char中的位数,一个char正好是一个
字节宽,每个对象必须是一整个字节宽。如果
CHAR_BIT为9,则对象必须是9位宽的倍数。
实现位数组的常用方法,如下:
1)决定你希望你的数组有多少位B(如果你在运行时决定这个
,你''我需要动态分配步骤2中的内存,
检查你是否已经得到它,并在完成后释放它;
2)分配(B + CHAR_BIT - 1)/ CHAR_BIT字节(unsigned char foo [N] = {0}
或unsigned char * foo = calloc((B + CHAR_BIT - 1)/ CHAR_BIT,1),
将它全部初始化为0(除非你动态分配
,否则你可以使用= {0},在这种情况下使用calloc - 极少数情况下的其中一个
这是一个好主意);
3)使用宏来获取,设置和测试单个位。
http://www.snippets.org 有一些宏可以用于这个pu rpose。
-
Richard Heathfield< http://www.cpax.org.uk>
电子邮件: -http:// WWW。 + rjh @
谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php>
Usenet是一个奇怪的放置" - dmr 1999年7月29日
这是CHAR_BIT而不是CHAR_BITS,忽略了,如果sizeof(任何东西)
== 2
我们知道它的大小是'char'的两倍。
Richard Heathfield< rj*@see.sig .invalidwrites:
丹尼尔·克拉夫特说:
个我需要实现类似于C ++的东西C中的std :: bitset;
< snip>
实现位数组的常用方法如下:
<剪断>
2)allocate(B + CHAR_BIT - 1)/ CHAR_BIT bytes(unsigned char
foo [N] = {0}
< snip>
可能值得添加使用无符号整数的原因
类型是这些移位操作定义明确,并且特别是使用unsigned char的原因是它保证
没有任何填充位。 br />
-
Ben。
Hi,
I do need to implement something similar to C++''s std::bitset in C; for
this, I use an array of int''s to get together any desired number of
bits, possibly larger than 32/64 or anything like this.
So of course it does not matter how many bits a single int has, but I do
need a reliable way to find it out.
I think of something like
CHAR_BITS*sizeof(int)
will do the trick, am I right here?
I''m just confused that it is *CHAR*_BITS; in reference to the usual
example, there are some machines where char''s have 9 bits--but is in
this case int required to have some multiple of 9 bits, too? I.e., does
sizeof(something) always give the size of this as multiples of
sizeof(char) or could such a 9 bit char be paired with 16/32 bit integers?
Thank you very much,
Daniel
--
Got two Dear-Daniel-Instant Messages
by MSN, associate ICQ with stress--so
please use good, old E-MAIL!
Daniel Kraft said:
Hi,
I do need to implement something similar to C++''s std::bitset in C; for
this, I use an array of int''s to get together any desired number of
bits, possibly larger than 32/64 or anything like this.
So of course it does not matter how many bits a single int has, but I do
need a reliable way to find it out.
I think of something like
CHAR_BITS*sizeof(int)
will do the trick, am I right here?Well, you mean CHAR_BIT, but yes, that will give you the total number of
bits occupied by the int - including the sign bit, at least (but possibly
more than) 15 value bits, and at least (but possibly more than) no padding
bits.
I''m just confused that it is *CHAR*_BITS; in reference to the usual
example, there are some machines where char''s have 9 bits--but is in
this case int required to have some multiple of 9 bits, too?Yes, CHAR_BIT gives the number of bits in a char, and a char is exactly one
byte wide, and every object must be a whole number of bytes wide. If
CHAR_BIT is 9, then objects must be a multiple of 9 bits wide.
The usual way to implement a "bit array", though, is as follows:
1) decide how many bits, B, you want your array to have (if you decide this
at runtime, you''ll need to allocate the memory in step 2 dynamically,
check that you''ve got it, and release it when you''re done);
2) allocate (B + CHAR_BIT - 1) / CHAR_BIT bytes (unsigned char foo[N] = {0}
or unsigned char *foo = calloc((B + CHAR_BIT - 1) / CHAR_BIT, 1),
initialising it all to 0 (you can use = {0} unless you allocate
dynamically, in which case use calloc - one of the rare occasions where
this is a good idea);
3) use macros to get, set, and test individual bits.
http://www.snippets.org has some macros that can be used for this purpose.
--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
It is CHAR_BIT and not CHAR_BITS, ignoring that, if sizeof(anything)
== 2
we know that it''s twice the size of a `char''.
Richard Heathfield <rj*@see.sig.invalidwrites:
Daniel Kraft said:
>I do need to implement something similar to C++''s std::bitset in C;
<snip>
The usual way to implement a "bit array", though, is as follows:<snip>
2) allocate (B + CHAR_BIT - 1) / CHAR_BIT bytes (unsigned char
foo[N] = {0}<snip>
It is probably worth adding the reason one uses an unsigned integer
type is that shift operations are well-defined on these, and the
reason one uses unsigned char in particular is that it is guaranteed
not to have any padding bits.
--
Ben.
这篇关于查找整数位数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!