This question already has an answer here:
这个问题在这里已有答案:
- Is it possible to create a data type of length one bit in C 8 answers
是否可以在C 8答案中创建长度为一位的数据类型
I need only one bit to represent my data - 1 or 0. What is the best way to do so in C? The "normal" data types are too large.
我只需要一位来表示我的数据 - 1或0.在C中这样做的最佳方法是什么? “正常”数据类型太大。
5 个解决方案
#1
14
You could create
你可以创造
typedef struct foo
{
unsigned x:1;
} foo;
Where you have told the compiler that you'll only be using one bit of x
.
你告诉编译器你只使用一位x的地方。
But due to structure packing arrangements (the C standard is intentionally flexible in order that compilers can optimise according to the machine architecture), it may well turn out that this still occupies as much space in memory as a regular unsigned
and an array of foo
s doesn't have to be bitwise contiguous.
但是由于结构打包安排(C标准是有意灵活的,以便编译器可以根据机器架构进行优化),很可能会发现这仍然占用了内存中与常规无符号和一系列foos无关的空间。不必按位连续。
#2
5
If you really want, you can create a structure with a member variable , bit-fielded to 1 bit.
如果您真的想要,可以使用成员变量创建一个结构,位为1位。
Remember, the data type of the member variable needs to be unsigned
, as you need to store 0
and 1
.
请记住,成员变量的数据类型需要是无符号的,因为您需要存储0和1。
#3
4
If you don't need millions of these flags or have extremely limited memory constraints, the best way is definitively an int
.
如果您不需要数百万个这样的标志或者内存限制非常有限,那么最好的方法肯定是int。
This is because an int
normally corresponds to the natural word size of your platform and can, properly aligned, be accessed quickly. The machine reads a word at a time anyways and using the single bits requires masking and shifting, that costs time. On your typical PC with gigabytes of RAM, this would be just silly.
这是因为int通常对应于平台的自然字大小,并且可以快速访问正确对齐的内容。无论如何,机器一次读取一个字,并且使用单个位需要屏蔽和移位,这需要花费时间。在具有千兆字节RAM的典型PC上,这将是愚蠢的。
If memory consumption really is an issue, there are bitfield structures.
如果内存消耗确实存在问题,则存在位域结构。
#4
1
The portable way is the definition of a variable which individual bits are used as flags.
可移植方式是变量的定义,其中各个位用作标志。
#define FLAG_FOO 0
#define FLAG_BAR 1
// in case platform does not support uint8_t
typedef unsigned char uint8_t;
uint8_t flags;
void flag_foo_set()
{
flags |= (1 << FLAG_FOO);
}
void flag_foo_clr()
{
flags &= ~(1 << FLAG_FOO);
}
uint8_t flag_foo_get()
{
return flags & (1 << FLAG_FOO);
}
While this can seem superfluos compared to C bit fields. It is portable to basically every ANSI C compiler.
虽然与C位字段相比,这似乎是超级流行的。它几乎可以移植到每个ANSI C编译器。
#5
0
Generally, the smallest addressable chunk of data in C is a byte. You can not have a pointer to a bit, so you can not declare a variable of 1 bit size. But as Sourav Ghosh already pointed out, you can declare bitfields, where one bit is accessed directly.
通常,C中最小的可寻址数据块是一个字节。您不能指向某个位,因此您无法声明1位大小的变量。但正如Sourav Ghosh已经指出的那样,你可以声明位域,直接访问一位。
#1
14
You could create
你可以创造
typedef struct foo
{
unsigned x:1;
} foo;
Where you have told the compiler that you'll only be using one bit of x
.
你告诉编译器你只使用一位x的地方。
But due to structure packing arrangements (the C standard is intentionally flexible in order that compilers can optimise according to the machine architecture), it may well turn out that this still occupies as much space in memory as a regular unsigned
and an array of foo
s doesn't have to be bitwise contiguous.
但是由于结构打包安排(C标准是有意灵活的,以便编译器可以根据机器架构进行优化),很可能会发现这仍然占用了内存中与常规无符号和一系列foos无关的空间。不必按位连续。
#2
5
If you really want, you can create a structure with a member variable , bit-fielded to 1 bit.
如果您真的想要,可以使用成员变量创建一个结构,位为1位。
Remember, the data type of the member variable needs to be unsigned
, as you need to store 0
and 1
.
请记住,成员变量的数据类型需要是无符号的,因为您需要存储0和1。
#3
4
If you don't need millions of these flags or have extremely limited memory constraints, the best way is definitively an int
.
如果您不需要数百万个这样的标志或者内存限制非常有限,那么最好的方法肯定是int。
This is because an int
normally corresponds to the natural word size of your platform and can, properly aligned, be accessed quickly. The machine reads a word at a time anyways and using the single bits requires masking and shifting, that costs time. On your typical PC with gigabytes of RAM, this would be just silly.
这是因为int通常对应于平台的自然字大小,并且可以快速访问正确对齐的内容。无论如何,机器一次读取一个字,并且使用单个位需要屏蔽和移位,这需要花费时间。在具有千兆字节RAM的典型PC上,这将是愚蠢的。
If memory consumption really is an issue, there are bitfield structures.
如果内存消耗确实存在问题,则存在位域结构。
#4
1
The portable way is the definition of a variable which individual bits are used as flags.
可移植方式是变量的定义,其中各个位用作标志。
#define FLAG_FOO 0
#define FLAG_BAR 1
// in case platform does not support uint8_t
typedef unsigned char uint8_t;
uint8_t flags;
void flag_foo_set()
{
flags |= (1 << FLAG_FOO);
}
void flag_foo_clr()
{
flags &= ~(1 << FLAG_FOO);
}
uint8_t flag_foo_get()
{
return flags & (1 << FLAG_FOO);
}
While this can seem superfluos compared to C bit fields. It is portable to basically every ANSI C compiler.
虽然与C位字段相比,这似乎是超级流行的。它几乎可以移植到每个ANSI C编译器。
#5
0
Generally, the smallest addressable chunk of data in C is a byte. You can not have a pointer to a bit, so you can not declare a variable of 1 bit size. But as Sourav Ghosh already pointed out, you can declare bitfields, where one bit is accessed directly.
通常,C中最小的可寻址数据块是一个字节。您不能指向某个位,因此您无法声明1位大小的变量。但正如Sourav Ghosh已经指出的那样,你可以声明位域,直接访问一位。