沉静
  
  首页 >>  
我的日历
分类日志
友情链接
最新评论
搜索日志
访问计数
获取 RSS
我的 Blog:
youalwayscan 最新的 20 条日志
[心情留言]
[点滴积累]
[好文共赏]
[C/C+基础]
[Unix/Linux基础]
[WxWidgets]
[VC/MFC]
全站 Blog:
全站最新的 20 条日志

 

Bit-fields

   C/C+基础2005-4-24 16:26
Imagine a fragment of a compiler that manipulates a symbol table. Each identifier in a program has certain information associated with it, for example, whether or not it is a keyword, whether or not it is external and/or static, and so on. The most compact way to encode such information is a set of one-bit flags in a single char or int.

The usual way this is done is to define a set of "masks" corresponding to the relevant bit positions, as in

#define KEYWORD 01
#define EXTRENAL 02
#define STATIC 04

or
enum { KEYWORD = 01, EXTERNAL = 02, STATIC = 04 };

The numbers must be powers of two. Then accessing the bits becomes a matter of "bit-fiddling" with the shifting, masking, and complementing operators.

Certain idioms appear frequently:

flags |= EXTERNAL | STATIC;

turns on the EXTERNAL and STATIC bits in flags, while
flags &= ~(EXTERNAL | STATIC);

turns them off, and
if ((flags & (EXTERNAL | STATIC)) == 0) ...

is true if both bits are off.

Although these idioms are readily mastered, as an alternative C offers the capability of defining and accessing fields within a word directly rather than by bitwise logical operators. A bit-field, or field for short, is a set of adjacent bits within a single implementation-defined storage unit that we will call a "word". For example, the symbol table #defines above could be replaced by the definition of three fields:

struct {
unsigned int is_keyword : 1;
unsigned int is_extern : 1;
unsigned int is_static : 1;
} flags;

This defines a variable table called flags that contains three 1-bit fields. The number following the colon represents the field width in bits. The fields are declared unsigned int to ensure that they are unsigned quantities.
Individual fields are referenced in the same way as other structure members: flags.is_keyword, flags.is_extern, etc. Fields behave like small integers, and may participate in arithmetic expressions just like other integers. Thus the previous examples may be written more naturally as

flags.is_extern = flags.is_static = 1;

to turn the bits on;
flags.is_extern = flags.is_static = 0;

to turn them off; and
if (flags.is_extern == 0 && flags.is_static == 0)
...

to test them.
标签集:TAGS:
回复Comments()点击Count()

回复Comments

{commenttime}{commentauthor}

{CommentUrl}
{commentcontent}