I am trying to learn more about this for implementation in my project.
Currently, I got this mostly:
unsigned char flags = 0; //8 bits flags |= 0x2; //apply random flag if(flags & 0x2) { printf("Opt 2 set"); }
Now I want to do a few more complicated things, what I want to do is apply three type flags:
flags = (0x1 | 0x2 | 0x4);
And then remove the 0x1 and 0x2 flags from it? I thought I could do something like this using bitwise NOT (and bitwise AND apply it):
flags &= ~(0x1 | 0x2);
Apparently they stay there or something anyway when I check.
I also don’t know how to check if they exist in the bit flags (so I can’t check if my previous code is working), would there be something like this?
if(flags & ~0x2) printf("flag 2 not set");
I can’t find any resources from my recent searches that apply to this, I’m ready to learn how to teach others, I’m really interested. I apologize if this is confusing or simple.
c ++ c boolean-operations bitflags
John
source share