在 C++ 中使用 #define 定义位标志

发布于 2024-10-21 03:11:16 字数 466 浏览 3 评论 0原文

我正在学习位标志。我已经知道它们是如何工作的以及它们是如何在 struct 中定义的。但是,我不确定它们是否可以在 #define 预处理器指令中定义,如下所示:

#define FLAG_FAILED:1

Is this preprocessor Define Directive the as a struct bit-flagDefinition?

PS:我已经遇到过这个相关问题,但它没有回答我的问题:#define 位标志和枚举 - 在“c”中和平共存。另外,如果您可以向我指出一些有关预处理器指令的信息,我将不胜感激。

I'm learning about bit-flags. I already know how they work and how they are defined in a struct. However, I'm unsure if they can be defined in a #define preprocessor directive like this:

#define FLAG_FAILED:1

Is this preprocessor define directive the as a struct bit-flag definition?

PS: I've already come across this related question but it didn't answer my question: #defined bitflags and enums - peaceful coexistence in "c". Also, if you can point me towards some information regarding preprocessor directives, I would appreciate that.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

萌无敌 2024-10-28 03:11:16

任何您想要用来将位标志注入到结构中的 #define 都必须采用以下形式:

#define IDENTIFIER SUBSTITUTED_CODE

在您假定的用途中...

#define FLAG_FAILED:1

标识符包含冒号,这使其无效。

你可以做这样的事情:

#define FLAG_FAILED int flag_failed :1

struct X
{
    char a;
    FLAG_FAILED;
    int b;
    ...
};

目前还不清楚为什么你要考虑使用位字段的定义。如果您只是想改变字段长度,那么:

#define FLAG_FAILED_BITS 1

struct X
{
    unsigned flag_failed :FLAG_FAILED_BITS;
};

...或...

#define FLAG_FAILED_BITS :1

struct X
{
    unsigned flag_failed FLAG_FAILED_BITS;
};

Any #define that you want to use to inject bitflags into a struct must take the form:

#define IDENTIFIER SUBSTITUTED_CODE

In your postulated use...

#define FLAG_FAILED:1

The identifier contains the colon, which makes it invalid.

You could do something like this:

#define FLAG_FAILED int flag_failed :1

struct X
{
    char a;
    FLAG_FAILED;
    int b;
    ...
};

It's not clear why you're considering using a define for the bit field anyway. If you just want to be able to vary the field length, then:

#define FLAG_FAILED_BITS 1

struct X
{
    unsigned flag_failed :FLAG_FAILED_BITS;
};

...or...

#define FLAG_FAILED_BITS :1

struct X
{
    unsigned flag_failed FLAG_FAILED_BITS;
};
扛起拖把扫天下 2024-10-28 03:11:16

#define FLAG_FAILED:1 并不是大多数人所知道的“位标志”意义上的真正位标志。这也是糟糕的语法。

通常定义位标志,以便您拥有一个类型,并通过“设置”它们来“打开”位。您可以通过“清除”标志来“关闭”它们。要比较位标志是否打开,您可以使用所谓的按位运算符AND(例如&)。

因此,您的 BIT0(例如 2^0)将定义BIT0 = 0x00000001,BIT1(例如 2^1)将定义为 BIT1 = 0x00000002。如果您想坚持使用定义,您可以通过设置和清除来实现:

#ifndef setBit
#define setBit(word, mask) word |= mask
#endif

#ifndef clrBit
#define clrBit(word, mask) word &= ~mask
#endif

或者作为模板

template<typename T>
inline T& setBit(T& word, T mask) { return word |= mask; }

template<typename T>
inline T& clrBit(T& word, T mask) { return word &= ~mask; }

如果您想设置位,可以这么说,您可以设置如下状态:

setBit(SystemState, SYSTEM_ONLINE);

setBit(SystemState,SYSTEM_ONLINE);

清除效果相同,只需替换 setBitclrBit

要进行比较,只需执行以下操作:

if (SystemState & SYSTEM_ONLINE) { ... // do some processing 

}

如果这是在 struct 中,则引用该 struct

#define FLAG_FAILED:1 is not really a bit flag in the sense that what most people know as a "bit flag". It's also bad syntax.

Bit flags typically are defined so that you have a type and you turn "on" bits by "setting" them. You turn them "off" by "clearing" the flag. To compare if the bit flag is on, you use what is called the bitwise operator AND (e.g. &).

So your BIT0 (e.g. 2^0) would be defined as BIT0 = 0x00000001 and BIT1 (e.g. 2^1) as BIT1 = 0x00000002. If you wanted to stick with define you could do it this way with setting and clearing:

#ifndef setBit
#define setBit(word, mask) word |= mask
#endif

#ifndef clrBit
#define clrBit(word, mask) word &= ~mask
#endif

or as a template

template<typename T>
inline T& setBit(T& word, T mask) { return word |= mask; }

template<typename T>
inline T& clrBit(T& word, T mask) { return word &= ~mask; }

If you want to set the bit, so to speak, you could have a state set as follows:

setBit(SystemState, SYSTEM_ONLINE);

or

setBit(SystemState, <insert type here>SYSTEM_ONLINE);

clearing would be the same just replace setBit with clrBit.

To compare, just do this:

if (SystemState & SYSTEM_ONLINE) { ... // do some processing 

}

if this is in a struct then, reference the struct.

み零 2024-10-28 03:11:16

使用 #define 宏定义按位值的形式是:

#define BIT_ONE    static_cast<int>( 1 << 0 )
#define BIT_TWO    static_cast<int>( 1 << 1 )
#define BIT_THREE  static_cast<int>( 1 << 2 )

A form to define bitwise values with #define macros is:

#define BIT_ONE    static_cast<int>( 1 << 0 )
#define BIT_TWO    static_cast<int>( 1 << 1 )
#define BIT_THREE  static_cast<int>( 1 << 2 )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文