何时使用位字段用C? [英] When to use bit-fields in C?

查看:108
本文介绍了何时使用位字段用C?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这个问题为什么我们需要使用位域',在谷歌,我发现该位字段用于标志。
现在我很好奇,是位字段被实际使用的唯一途径?
我们是否需要使用位字段来节省空间?

这本书定义位域的方式:

 结构{
unsigned int类型is_keyword:1;
unsigned int类型is_extern:1;
unsigned int类型is_static:1;
}标志;

为什么我们使用INT?多少空间被占用?我很困惑,为什么我们使用int,但不能短期或较史密斯小INT。据我了解只有1位被占用在内存中,而不是整个无符号整型值。难道是正确的吗?


解决方案

  

现在我很好奇,[是标志]位字段是实际使用的唯一途径?


没有,标志没有被使用的唯一途径位字段。它们也可以被用来存储多个比特值越大,虽然标志是比较常见的。例如:

 的typedef枚举{
    北= 0,
    EAST = 1,
    南= 2,
    WEST = 3
} directionValues​​;结构{
    unsigned int类型alice_dir:2;
    unsigned int类型bob_dir:2;
}方向;


  

我们需要使用位字段来节省空间?


位域也节省空间。它们还允许一个更简单的方法来设置没有字节对齐的值。而不是位移,并使用位运算,我们可以使用相同的语法在结构设置字段。这提高了可读性。有位域,你可以写

  directions.bob_dir = SOUTH;

不过,做手工,你需要写类似:

 的#define BOB_OFFSET 2
方向与安培; =〜(3';< BOB_OFFSET); //明确Bob的位
路线| =南亚及LT;< BOB_OFFSET;

这提高可读性无疑是比在这里和那里节省几个字节更重要。


  

为什么我们使用INT?多少空间被占用?


整个空间 INT 被占用。我们使用 INT ,因为在很多情况下,它其实并不重要。如果,对于一个单一的值,可以使用4个字节,而不是1或2,用户可能不会注意到。对于某些平台,大小不事多了,你可以使用占用空间少其他数据类型(字符 uint8_t有等)。


  

据我所知,只有1位被占用在内存中,而不是整个无符号整型值。难道是正确的吗?


没有,这是不正确的。整个 unsigned int类型会存在,即使你只使用其位8。

On the question 'why do we need to use bit-fields', in Google I found that bit fields are used for flags. Now I am curious, is it the only way bit-fields are used practically? Do we need to use bit fields to save space?

Way of defining bit field from the book:

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

Why do we use int? How much space is occupied? I am confused why we are using int, but not short or smith smaller than int. As I understand only 1 bit is occupied in memory, but not the whole unsigned int value. Is it correct?

解决方案

Now I am curious, [are flags] the only way bit-fields are used practically?

No, flags are not the the only way bit-fields are used. They can also be used to store values larger than one bit, although flags are more common. For instance:

typedef enum {
    NORTH = 0,
    EAST = 1,
    SOUTH = 2,
    WEST = 3
} directionValues;

struct {
    unsigned int alice_dir : 2;
    unsigned int bob_dir : 2;
} directions;

Do we need to use bit fields to save space?

Bit fields do save space. They also allow an easier way to set values that aren't byte-aligned. Rather than bit-shifting and using bitwise operations, we can use the same syntax as setting fields in a struct. This improves readability. With a bitfield, you could write

directions.bob_dir = SOUTH;

However, doing it manually, you would need to write something like:

#define BOB_OFFSET 2
directions &= ~(3<<BOB_OFFSET); // clear Bob's bits
directions |= SOUTH<<BOB_OFFSET;

This improved readability is arguably more important than saving a few bytes here and there.

Why do we use int? How much space is occupied?

The space of an entire int is occupied. We use int because in many cases, it doesn't really matter. If, for a single value, you use 4 bytes instead of 1 or 2, your user probably won't notice. For some platforms, size does matter more, and you can use other data types which take up less space (char, short, uint8_t, etc).

As I understand only 1 bit is occupied in memory, but not the whole unsigned int value. Is it correct?

No, that is not correct. The entire unsigned int will exist, even if you're only using 8 of its bits.

这篇关于何时使用位字段用C?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆