如何在C++中优雅地处理位掩码
How can I treat bit mask graceful in C++?
我遇到过这样的代码。
#define JOB_STATUS_PAUSED 0x00000001
#define JOB_STATUS_ERROR 0x00000002
#define JOB_STATUS_DELETING 0x00000004
#define JOB_STATUS_SPOOLING 0x00000008
#define JOB_STATUS_PRINTING 0x00000010
#define JOB_STATUS_OFFLINE 0x00000020
#define JOB_STATUS_PAPEROUT 0x00000040
#define JOB_STATUS_PRINTED 0x00000080
#define JOB_STATUS_DELETED 0x00000100
#define JOB_STATUS_BLOCKED_DEVQ 0x00000200
#define JOB_STATUS_USER_INTERVENTION 0x00000400
#define JOB_STATUS_RESTART 0x00000800
DWORD func();
函数返回一个双字,它是这些位掩码的组合。我想测试返回值的状态。我这样写代码。
if(ret&JOB_STATUS_PAUSED)
string str="JOB_STATUS_PAUSED";
if(ret&JOB_STATUS_ERROR)
string str="JOB_STATUS_ERROR";
我想知道有没有一种优雅的方法来治疗口罩?我认为std::bitset是不够的,我还需要获取宏的字符串。我认为这个宏可以帮助,但我不知道如何使用它
#define str(a) #a
//if I input str(JOB_STATUS_PAUSED) then I can get "JOB_STATUS_PAUSED"
#define str(a) L#a
const wchar_t* statusStr[]{
str(JOB_STATUS_PAUSED),
str(JOB_STATUS_ERROR),
str(JOB_STATUS_DELETING),
str(JOB_STATUS_SPOOLING),
str(JOB_STATUS_PRINTING),
str(JOB_STATUS_OFFLINE),
str(JOB_STATUS_PAPEROUT),
str(JOB_STATUS_PRINTED),
str(JOB_STATUS_DELETED),
str(JOB_STATUS_BLOCKED_DEVQ),
str(JOB_STATUS_USER_INTERVENTION),
str(JOB_STATUS_RESTART)
};
int len = dimof1(statusStr);
std::vector<std::wstring> ret;
for (int i = 0, init = 1; i < len; i++) {
if (status & init) {
ret.push_back(statusStr[i]);
}
init = init << 1;
}
return ret;
#undef str
我认为这符合我的要求。
相关文章:
- 位移操作和位掩码未检测到重复字符
- OpenCV - 带有掩码的absdiff
- 生成前缀位掩码
- 如何从__m64值的 lsb 创建 8 位掩码?
- 如何对无符号长 int 进行位掩码?
- 删除K的背景掩码-意味着Python或C++中的集群/
- 如何在C++中优雅地处理位掩码
- 将uint64_t位掩码转换为 std::布尔数组
- 使输入二进制掩码适应 ITK 网格生成器
- 如何从 getifaddr 读取子网掩码
- 优化从子位掩码生成父位掩码
- 基于模式创建位掩码作为 constexpr
- 使用二进制掩码 C++ ITK 获取感兴趣区域
- C++中的运行时位复制(位掩码)
- 根据 IP 和掩码C++打印所有 IP
- C++设置"blank"或重置 ifstrean (ios) 的异常掩码
- OpenCV 检测带有掩码的斑点
- OPENCV:如何创建多边形形状的掩码
- 递归函数,用于使用位掩码 c++ 显示集合的所有子集
- Istream异常掩码和处理