Bitset宏 [英] Bitset Macros
问题描述
我在某处找到了一些看起来大致如下的位宏;
#define bitset_elem(ptr,bit)((unsigned char *)(ptr))[(bit )/ b]
#define bitset_mask(ptr,bit)(1<<(bit)%8)
#define bitset_isset(ptr, bit((bitset_elem(ptr,bit)& bitset_mask(ptr,bit))!= 0)
#define bitset_set(ptr,bit)(bitset_elem(ptr,bit)| = bitset_mask( ptr,bit))
#define bitset_unset(ptr,bit)(bitset_elem(ptr,bit)& = ~bitset_mask(ptr,bit))
#define bitset_toggle (ptr,bit)(bitset_elem(ptr,bit)^ = bitset_mask(ptr,bit))
这些是否合法,有人可以推荐改进吗?
Mike
I found some bit set macros somewhere that look roughly like the following;
#define bitset_elem(ptr,bit) ((unsigned char *)(ptr))[(bit)/8]
#define bitset_mask(ptr,bit) (1 << (bit) % 8)
#define bitset_isset(ptr,bit) ((bitset_elem(ptr,bit) & bitset_mask(ptr,bit)) != 0)
#define bitset_set(ptr,bit) (bitset_elem(ptr,bit) |= bitset_mask(ptr,bit))
#define bitset_unset(ptr,bit) (bitset_elem(ptr,bit) &= ~bitset_mask(ptr,bit))
#define bitset_toggle(ptr,bit) (bitset_elem(ptr,bit) ^= bitset_mask(ptr,bit))
Are these legal and can anyone recommend improvements?
Mike
推荐答案
开始跟进Michael B Allen:
begin followup to Michael B Allen:
我在某个地方找到了一些位设置的宏大致类似于
以下;
#define bitset_elem(ptr,bit)((unsigned char *)(ptr))[(bit)/ 8]
#define bitset_mask( ptr,bit)(1<< (位)%8)
I found some bit set macros somewhere that look roughly like the
following;
#define bitset_elem(ptr,bit) ((unsigned char *)(ptr))[(bit)/8]
#define bitset_mask(ptr,bit) (1 << (bit) % 8)
一个明显的问题是使用来自limits.h的CHAR_BIT而不是硬的
有线'8' '。
-
$ b $bFürGoogle,Tux和GPL!
One obvious issue is to use CHAR_BIT from limits.h instead of a hard
wired ''8''.
--
Für Google, Tux und GPL!
" Alexander Bartolich" ; <人***************** @ gmx.at>在消息中写道
news:bs ************ @ ID-193444.news.uni-berlin.de ...
"Alexander Bartolich" <al*****************@gmx.at> wrote in message
news:bs************@ID-193444.news.uni-berlin.de...
开始迈克尔·艾伦的后续跟踪:
begin followup to Michael B Allen:
我发现某些位置的宏看起来大致类似于
以下;
#define bitset_elem(ptr,bit)(( unsigned char *)(ptr))[(bit)/ 8]
#define bitset_mask(ptr,bit)(1<<(bit)%8)
I found some bit set macros somewhere that look roughly like the
following;
#define bitset_elem(ptr,bit) ((unsigned char *)(ptr))[(bit)/8]
#define bitset_mask(ptr,bit) (1 << (bit) % 8)
>一个明显的问题是使用来自limits.h的CHAR_BIT而不是硬连接'8''。
One obvious issue is to use CHAR_BIT from limits.h instead of a hard
wired ''8''.
另一个是使用1u而不是1,以满足系统的需要
UCHAR_MAX> INT_MAX。
但我实际上不得不质疑ptr是什么类型以及为什么作者感觉需要(unsigned char *)强制转换。
[就个人而言,我认为标准可以通过定义
UINT_BIT和ULONG_BIT等来帮助这些情况......以表示(符号+)的数量值位
在给定的整数类型中,而不仅仅是unsigned char。]
-
Peter
The other would be to use 1u instead of 1, to cater for systems where
UCHAR_MAX > INT_MAX.
But I''d actually have to question what type ptr is and why the author feels
the need for the (unsigned char *) cast.
[Personally, I think the standard could help in these situations by defining
UINT_BIT and ULONG_BIT etc... to indiciate the number of (sign+) value bits
in a given integer type, not just unsigned char.]
--
Peter
2003年12月31日星期三17:37:17 -0500,Alexander Bartolich写道:
On Wed, 31 Dec 2003 17:37:17 -0500, Alexander Bartolich wrote:
开始跟进Michael B Allen:
begin followup to Michael B Allen:
我在某个地方发现了一些看起来大致类似于
的位置;
#define bitset_elem(ptr,bit)((unsigned char *)(ptr))[ (位)/ 8]
#define bitset_mask(ptr,bit)(1<<(bit)%8)
I found some bit set macros somewhere that look roughly like the
following;
#define bitset_elem(ptr,bit) ((unsigned char *)(ptr))[(bit)/8]
#define bitset_mask(ptr,bit) (1 << (bit) % 8)
一个明显的问题是使用CHAR_BIT来自limits.h而不是硬连接''8''。
One obvious issue is to use CHAR_BIT from limits.h instead of a hard
wired ''8''.
对我来说并不明显。是不是保证是8位?
Mike
Not obvious to me. Isn''t char guaranteed to be 8 bits?
Mike
这篇关于Bitset宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!