Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple boolean operators for bit flags

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):

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.

like image 275
John Avatar asked Nov 16 '10 06:11

John


People also ask

How do you use bit flags in C++?

Setting Up Bit Flags Values/States To comply with this you will only want to use powers of 2 to represent each state. An easy way to do this is with the 1 << N operator, where N would be the current state count, this way you use the next bit every time, such that the state values would be 1, 2, 4, 8, 16… and so on.

Do Bitwise Operators work with Booleans?

Along with integer operands, the bitwise OR can also be used with boolean operands. It returns true if at least one of the operands is true, otherwise, it returns false.

What do Bitwise Operators do?

Bitwise operators are characters that represent actions (bitwise operations) to be performed on single bits. They operate at the binary level and perform operations on bit patterns that involve the manipulation of individual bits.


2 Answers

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.

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:

if ((flags & (0x1 | 0x2)) == 0)
    printf("flags 1 and 2 are not set (but maybe only one of them is!)\n");
like image 88
cdhowie Avatar answered Sep 22 '22 14:09

cdhowie


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) ...
like image 20
paxdiablo Avatar answered Sep 21 '22 14:09

paxdiablo