什么时候在 C 中使用位域?

When to use bit-fields in C?(什么时候在 C 中使用位域?)

本文介绍了什么时候在 C 中使用位域?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于'为什么我们需要使用位域'的问题,在谷歌上搜索我发现位域用于标志.现在我很好奇,

  1. 这是实际使用位域的唯一方式吗?
  2. 我们是否需要使用位域来节省空间?

书中定义位域的方式:

struct {无符号整数 is_keyword : 1;无符号整数 is_extern : 1;无符号整数 is_static : 1;} 标志;

  1. 为什么要使用 int?
  2. 占用了多少空间?

我很困惑为什么我们使用 int,而不是 short 或比 int 更小的东西.

  1. 据我所知,内存中只占用了 1 位,而不是整个 unsigned int 值.正确吗?

解决方案

现在我很好奇,[是标志]实际使用位域的唯一方式吗?

不,标志不是使用位域的唯一方式.它们也可用于存储大于一位的值,尽管标志更为常见.例如:

typedef enum {北 = 0,东 = 1,南 = 2,西 = 3} 方向值;结构{无符号整数 alice_dir : 2;无符号整数 bob_dir : 2;} 方向;

<块引用>

我们是否需要使用位域来节省空间?

位域确实可以节省空间.它们还提供了一种更简单的方法来设置非字节对齐的值.我们可以使用与在 struct 中设置字段相同的语法,而不是位移和使用按位运算.这提高了可读性.使用位域,你可以写

directions.alice_dir = WEST;方向.bob_dir = 南;

但是,要在一个 int(或其他类型)的空间中存储多个没有位字段的独立值,您需要编写如下内容:

#define ALICE_OFFSET 0#define BOB_OFFSET 2方向 &= ~(3<

可以说,提高位字段的可读性比在这里和那里保存几个字节更重要.

<块引用>

为什么要用int?占用了多少空间?

整个int的空间被占用.我们使用 int 是因为在很多情况下,这并不重要.如果对于单个值,您使用 4 个字节而不是 1 个或 2 个,您的用户可能不会注意到.对于某些平台,大小更重要,您可以使用占用较少空间的其他数据类型(charshortuint8_t 等).

<块引用>

据我所知,内存中只占用了 1 位,而不是整个 unsigned int 值.正确吗?

不,这是不正确的.整个 unsigned int 将存在,即使您只使用它的 8 位.

On the question 'why do we need to use bit-fields', searching on Google I found that bit fields are used for flags. Now I am curious,

  1. Is it the only way bit-fields are used practically?
  2. 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;

  1. Why do we use int?
  2. How much space is occupied?

I am confused why we are using int, but not short or something smaller than an int.

  1. 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.alice_dir = WEST;
directions.bob_dir = SOUTH;

However, to store multiple independent values in the space of one int (or other type) without bit-fields, you would need to write something like:

#define ALICE_OFFSET 0
#define BOB_OFFSET 2
directions &= ~(3<<ALICE_OFFSET); // clear Alice's bits
directions |= WEST<<ALICE_OFFSET; // set Alice's bits to WEST
directions &= ~(3<<BOB_OFFSET);   // clear Bob's bits
directions |= SOUTH<<BOB_OFFSET;  // set Bob's bits to SOUTH

The improved readability of bit-fields 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 中使用位域?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:什么时候在 C 中使用位域?

基础教程推荐