检查是否在整数变量中设置了标志
Check if flag is set in integer variable
我正在制作自己的简单绘图引擎。我正在尝试使用我认为称为按位比较的方法确定变量是否已设置为特定值,但我可能错了。
我一直对以下内容是什么以及如何使用它感到有些困惑:
int DRAW_REPEAT_X = 70001; // I have a feeling I should make this value binary instead of a unique number, ie, 0
int DRAW_REPEAT_Y = 70002; // I have a feeling I should make this value binary instead of a unique number, ie, 2
int drawMethod = DRAW_REPEAT_X | DRAW_REPEAT_Y; // this means I want to repeat an image on both the x and y axis doesn't it?
// Now I want to check if drawMethod has DRAW_REPEAT_X set: this is where I struggle to know how to check this
// Is the following correct?
if (drawMethod && DRAW_REPEAT_X) {
// the user wants me to repeat an image along the x axis
}
// Now I want to check if drawMethod has DRAW_REPEAT_Y set: this is where I struggle to know how to check this
if (drawMethod && DRAW_REPEAT_Y) {
// the user wants me to repeat an image along the x axis
}
以下代码是否正确检查是否设置了DRAW_REPEAT_X?它总是在我的检查中返回 1。
编辑要检查是否设置了两个位,我这样做吗?
if (drawMethod & DRAW_REPEAT_X & DRAW_REPEAT_Y) {
// both set
}
// OR
if (drawMethod & DRAW_REPEAT_X && drawMethod & DRAW_REPEAT_Y) {
// both set
}
为此,您的标志变量都需要设置一个唯一的位。 那位就是"旗帜"。 对于按位表示很重要的常量,使用十六进制或八进制(因为这些基数是 2 的幂)比使用十进制要方便得多。 因此,例如:
enum {
DRAW_REPEAT_X = 0x01, /* First bit set */
DRAW_REPEAT_Y = 0x02, /* Second bit set */
DRAW_MIRRORED = 0x04, /* Third bit set */
};
int drawMethod = DRAW_REPEAT_X | DRAW_REPEAT_Y; /* Will have both first and second bits set */
然后,使用按位和&
而不是逻辑和&&
来测试位。 当且仅当在 a
和 b
中都设置了至少一个位时,a & b
将为非零。 在测试标志的情况下,其中一个将只设置一个位 - 你感兴趣的标志 - 所以a & flag
的结果将是非零当且仅当标志设置为a
:
if (drawMethod & DRAW_REPEAT_X) {
// the user wants me to repeat an image along the x axis
}
if (drawMethod & DRAW_REPEAT_Y) {
// the user wants me to repeat an image along the x axis
}
设置了一位的常量的十六进制模式是 0x01
、 0x02
、 0x04
、 0x08
、 0x10
、 0x20
、 ...
不,它不是,您应该改用按位 AND 运算符 - &
并将标志设置为二进制值 - 您的直觉在那一边是正确的。
设置特定位的一个常见技巧是使用 shift 运算符:
int DRAW_REPEAT_X = 0x1 << 0; //first bit set to 1, others 0
int DRAW_REPEAT_Y = 0x1 << 1; //second bit set to 1, others 0
并检查 int 为
if (drawMethod & DRAW_REPEAT_X) //check it that particular flag is set, ignore others
{
}
就目前而言,您与其说是使用标志,不如说是具有指示方法的值。更好的是使用某种位,如下所示:
int DRAW_REPEAT_X=0x01;
int DRAW_REPEAT_Y=0x02;
然后像你现在一样检查ifs,但用一个>
if (drawMethod & DRAW_REPEAT_X)
通常,如果你使用类体系结构,你的整数(DRAW_REPEAT_X
)应该是public static
的。但不知道是不是这样,我不会包括它们
使用 WinAPI 的代码片段,演示如何将两个标志设置为一个值,然后检查该值中是否存在至少一个标志。它应该return 0;
INPUT mip;
mip.type = INPUT_MOUSE;
mip.mi.mouseData = 0;
mip.mi.dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE;
if (mip.mi.dwFlags & (MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_HWHEEL))
return 0;
如果要检查精确的值组合,则无需使用按位运算符 &
,并且可以执行简单的==
检查。
例如,底部附近的更新行
INPUT mip;
mip.type = INPUT_MOUSE;
mip.mi.mouseData = 0;
mip.mi.dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE;
if (mip.mi.dwFlags == (MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE))
return 0;
- 在 CMake 中为每个目标设置编译器/链接器标志
- 如何正确设置 ios 标志以进行流操作?
- 有关使用标志位设置内部错误标志的说明
- 如何在CMakeLists.txt中正确设置CMake标志
- 设置用于调试的 g++ 编译器标志
- 如何在Qt中取消设置标志?
- SDL_CreateWindow在设置标志时失败SDL_WINDOW_VULKAN
- 如何在 cmake 中设置编译器特定的标志
- 如何在图像位中设置标志以标记音频文件的结尾
- 添加 swig pythoncode 以在 Python 对象上设置自己的标志
- 如何在cmake中的源文件之后设置库标志?
- 为什么我必须将错误状态标志设置为 goodbit 才能使其工作
- while 循环是否在设置标志的情况下运行多次?
- 设置 Xcode 标志以便将 python 嵌入我的C++项目中
- 如何嵌入在C++中设置标志的 python 代码
- 检索自最后n秒以来设置标志的次数的有效方法
- 在信号处理程序中设置标志
- 如何在 c++ 中设置标志的格式
- 说双向文件声明不隐式设置标志是否正确
- 如何为 fstream 对象设置标志FILE_FLAG_BACKUP_SEMANTICS