检查是否在整数变量中设置了标志

Check if flag is set in integer variable

本文关键字:设置 标志 变量 是否 整数 检查      更新时间:2023-10-16

我正在制作自己的简单绘图引擎。我正在尝试使用我认为称为按位比较的方法确定变量是否已设置为特定值,但我可能错了。

我一直对以下内容是什么以及如何使用它感到有些困惑:

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 */

然后,使用按位和&而不是逻辑和&&来测试位。 当且仅当在 ab 中都设置了至少一个位时,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
}

设置了一位的常量的十六进制模式是 0x010x020x040x080x100x20 、 ...

不,它不是,您应该改用按位 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;