I am attempting to learn more about this to implement in my project.
我正试图在我的项目中了解更多这方面的知识。
I currently have got this basically:
我目前基本上有:
unsigned char flags = 0; //8 bits
flags |= 0x2; //apply random flag
if(flags & 0x2) {
printf("Opt 2 set");
}
Now I am wishing to do a little more complex things, what I am wanting to do is apply three flags like this:
现在我想做一些更复杂的事情,我想做的是应用三个标志:
flags = (0x1 | 0x2 | 0x4);
And then remove flags 0x1
and 0x2
from it? I thought I could do something like this applying bitwise NOT (and bitwise AND to apply it):
然后从它上移除标记0x1和0x2 ?我想我可以做一些类似的事情,应用bitwise,而不是(和应用它):
flags &= ~(0x1 | 0x2);
Apparently they remain there or something either way when I check.
很明显,当我检查的时候,他们还在那儿或者别的什么。
I also do not know how to check if they do NOT exist in the bit flags (so I cannot check if my previous code works), would it be something like this?
我也不知道如何检查它们是否在位标志中不存在(因此我无法检查我以前的代码是否有效),会是这样的吗?
if(flags & ~0x2)
printf("flag 2 not set");
I can not find any resources from my recent searches that apply to this, I am willing to learn this to teach others, I am really interested. I apologize if this is confusing or simple.
我在最近的搜索中找不到任何适用于这个的资源,我很愿意学习这个来教别人,我真的很感兴趣。如果这让人困惑或简单,我很抱歉。
2 个解决方案
#1
21
And the remove two from it? I thought I could do something like this:
然后从它上面移除两个?我想我可以这样做:
flags &= ~(0x1 | 0x2);
to remove those two flags, but apparently they remain there or something either way.
移除这两面旗帜,但显然它们仍然存在,或者其他任何方式。
That is the correct way to remove flags. If you printf("%d\n", flags)
after that line, the output should be 4
.
这是删除标志的正确方式。如果您在这一行之后printf(“%d\n”,flags),输出应该是4。
I also do not know how to check if they do NOT exist in the bit flag (so I cannot check if my previous code works), would it be something like this?
我也不知道如何检查它们是否在位标志中不存在(因此我无法检查我以前的代码是否工作),它会是这样的吗?
if(flags & ~0x2) printf("flag 2 not set");
Nope:
不:
if ((flags & 0x2) == 0)
printf("flag 2 not set");
EDIT:
编辑:
To test for the presence of multiple flags:
测试是否存在多个标志:
if ((flags & (0x1 | 0x2)) == (0x1 | 0x2))
printf("flags 1 and 2 are set\n");
To test for the absence of multiple flags, just compare to 0 as before:
若要测试是否缺少多个标志,只需与之前的0进行比较:
if ((flags & (0x1 | 0x2)) == 0)
printf("flags 1 and 2 are not set (but maybe only one of them is!)\n");
#2
11
I'm not sure why you think that clearing operation won't work.
我不知道你为什么认为清理工作行不通。
flags &= ~(0x1 | 0x2);
is the correct way to do it. The operation to check if a bit isn't set is:
是正确的方法。检查位是否未设置的操作为:
if (!(flags & 0x2)) ...
The one you have:
你有:
if (flags & ~0x2) ...
will be true if any other bit is set, which is probably why you thing the clearing operation isn't working. The problem lies not with the clearing but with the checking.
如果设置了其他任何位,将是正确的,这可能是您认为清理操作不起作用的原因。问题不在于清算,而在于检查。
If you want to check that all bits in a group are set:
如果要检查组中的所有位都已设置:
if ((flags & (0x2|0x1)) == 0x2|0x1) ...
#1
21
And the remove two from it? I thought I could do something like this:
然后从它上面移除两个?我想我可以这样做:
flags &= ~(0x1 | 0x2);
to remove those two flags, but apparently they remain there or something either way.
移除这两面旗帜,但显然它们仍然存在,或者其他任何方式。
That is the correct way to remove flags. If you printf("%d\n", flags)
after that line, the output should be 4
.
这是删除标志的正确方式。如果您在这一行之后printf(“%d\n”,flags),输出应该是4。
I also do not know how to check if they do NOT exist in the bit flag (so I cannot check if my previous code works), would it be something like this?
我也不知道如何检查它们是否在位标志中不存在(因此我无法检查我以前的代码是否工作),它会是这样的吗?
if(flags & ~0x2) printf("flag 2 not set");
Nope:
不:
if ((flags & 0x2) == 0)
printf("flag 2 not set");
EDIT:
编辑:
To test for the presence of multiple flags:
测试是否存在多个标志:
if ((flags & (0x1 | 0x2)) == (0x1 | 0x2))
printf("flags 1 and 2 are set\n");
To test for the absence of multiple flags, just compare to 0 as before:
若要测试是否缺少多个标志,只需与之前的0进行比较:
if ((flags & (0x1 | 0x2)) == 0)
printf("flags 1 and 2 are not set (but maybe only one of them is!)\n");
#2
11
I'm not sure why you think that clearing operation won't work.
我不知道你为什么认为清理工作行不通。
flags &= ~(0x1 | 0x2);
is the correct way to do it. The operation to check if a bit isn't set is:
是正确的方法。检查位是否未设置的操作为:
if (!(flags & 0x2)) ...
The one you have:
你有:
if (flags & ~0x2) ...
will be true if any other bit is set, which is probably why you thing the clearing operation isn't working. The problem lies not with the clearing but with the checking.
如果设置了其他任何位,将是正确的,这可能是您认为清理操作不起作用的原因。问题不在于清算,而在于检查。
If you want to check that all bits in a group are set:
如果要检查组中的所有位都已设置:
if ((flags & (0x2|0x1)) == 0x2|0x1) ...