C语言 最小的数据类型 - 我可以定义一位变量吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31310496/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 12:02:26  来源:igfitidea点击:

Smallest data type - can I define a one bit variable?

ctypes

提问by Roy

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 中这样做的最佳方法是什么?“正常”数据类型太大。

回答by Bathsheba

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 unsignedand an array of foos doesn't have to be bitwise contiguous.

但是由于结构包装安排(C 标准有意灵活,以便编译器可以根据机器架构进行优化),结果很可能仍然占用与常规unsignedfoos数组一样多的内存空间' t 必须按位连续。

回答by Sourav Ghosh

If you really want, you can create a structure with a member variable , bit-fieldedto 1 bit.

如果你真的想要,你可以创建一个带有成员变量的结构,位域为 1 位。

Remember, the data type of the member variable needs to be unsigned, as you need to store 0and 1.

请记住,成员变量的数据类型必须是unsigned,因为您需要存储01

回答by Sourav Ghosh

If you don't need millionsof these flags or have extremely limited memory constraints, the best way is definitively an int.

如果您不需要数百万个这样的标志或内存限制极其有限,那么最好的方法绝对是int.

This is because an intnormally 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.

这是因为 aint通常对应于您平台的自然字长,并且可以正确对齐并快速访问。无论如何,机器一次读取一个字,使用单个位需要屏蔽和移位,这会花费时间。在具有 GB 内存的典型 PC 上,这很愚蠢。

If memory consumption reallyis an issue, there are bitfield structures.

如果内存消耗确实是一个问题,则可以使用位域结构。

回答by Sourav Ghosh

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 编译器。

回答by jwsc

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 已经指出的那样,您可以声明位域,直接访问一位。