繁体   English   中英

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

[英]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? 在我的anding支票中,它总是返回1。

编辑并检查是否两个位都设置了吗?

if (drawMethod & DRAW_REPEAT_X & DRAW_REPEAT_Y) {
   // both set
}

// OR

if (drawMethod & DRAW_REPEAT_X && drawMethod & DRAW_REPEAT_Y) {
   // both set
}

不,不是,您应该使用按位AND运算符- &并将标志设置为二进制值-您的直觉在那一侧是正确的。

设置特定位的常见技巧是使用移位运算符:

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

并检查为

if (drawMethod & DRAW_REPEAT_X)  //check it that particular flag is set, ignore others
{
}

为此,您的标志变量每个都需要具有一个唯一的位集。 该位是“标志”。 对于重要的是按位表示形式的常量,使用十六进制或八进制(因为这些基数是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 ,...

就目前而言,您所使用的标志并不像具有指示方法的值那样。 更好的方法是使用某种位,例如:

int DRAW_REPEAT_X=0x01;
int DRAW_REPEAT_Y=0x02;

然后像现在一样检查是否存在,但使用一个&

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;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM