我正在制作自己的简单绘图引擎。我正在尝试使用我认为所谓的按位比较来确定变量是否已设置为特定值,但我可能错了。
我一直对以下是什么以及如何使用它感到有些困惑:
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
}