使用C++中的宏进行二进制操作

ble*_*ech 0 c++ binary macros

我想通过宏使用二进制标志,但在以下方面遇到编译器错误:

#define FLAG_A 0x01;
#define FLAG_B 0x02;

int binVal = 0;
binVal = FLAG_A | FLAG_B;

//getting 0x03 here
Run Code Online (Sandbox Code Playgroud)

但编译器以:

error: expected primary-expression before ‘|’ token
Run Code Online (Sandbox Code Playgroud)

尝试:

binVal = 0x01 | 0x02; //this does perfectly well.
Run Code Online (Sandbox Code Playgroud)

感谢您的回复.

Luc*_*ore 8

删除分号:

#define FLAG_A 0x01
#define FLAG_B 0x02
Run Code Online (Sandbox Code Playgroud)

否则该声明将扩展为

binVal = 0x01; | 0x02;;
Run Code Online (Sandbox Code Playgroud)