When to use bit-fields in C?(什么时候在 C 中使用位域?)
问题描述
关于'为什么我们需要使用位域'的问题,在谷歌上搜索我发现位域用于标志.现在我很好奇,
- 这是实际使用位域的唯一方式吗?
- 我们是否需要使用位域来节省空间?
书中定义位域的方式:
struct {无符号整数 is_keyword : 1;无符号整数 is_extern : 1;无符号整数 is_static : 1;} 标志;
- 为什么要使用 int?
- 占用了多少空间?
我很困惑为什么我们使用 int
,而不是 short
或比 int
更小的东西.
- 据我所知,内存中只占用了 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 个,您的用户可能不会注意到.对于某些平台,大小更重要,您可以使用占用较少空间的其他数据类型(char
、short
、uint8_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,
- 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 something smaller than an 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.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 中使用位域?
基础教程推荐
- Windows Media Foundation 录制音频 2021-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 从 std::cin 读取密码 2021-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01