Managing a set of boolean flags using bitwise

?

-

Bitwise operators can be used to manage a set of boolean flags.
Would one bother to do so or is it simpler to just use separate flags?
 
R

Roedy Green

Bitwise operators can be used to manage a set of boolean flags.
Would one bother to do so or is it simpler to just use separate flags?

you can manage 64 flags in one machine cycle. Doing them separately
would take at least 64 times longer. Your code can be much shorter.

--
Bush crime family lost/embezzled $3 trillion from Pentagon.
Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
http://www.infowars.com/articles/us/mckinney_grills_rumsfeld.htm

Canadian Mind Products, Roedy Green.
See http://mindprod.com/iraq.html photos of Bush's war crimes
 
S

Stefan Schulz

you can manage 64 flags in one machine cycle. Doing them separately
would take at least 64 times longer. Your code can be much shorter.

On the other hand, it also gets more confusing by orders of magnitude, and
the testing for each flag does not involve a simple "branch if zero"
instruction, but "and with constant, branch if zero". Pick your poison.

However, if that was the time-critical part of your app, then i would
really be shocked. And as you know, premature optimization is the root of
all evil
 
R

Roedy Green

On the other hand, it also gets more confusing by orders of magnitude, and
the testing for each flag does not involve a simple "branch if zero"
instruction, but "and with constant, branch if zero". Pick your poison.

Once you understand bit masks, the code is easier to read. If you
don't it is totally mysterious. It depends on your audience.

the bitmask code works with a number of stereotypical operations.
Individual bit fiddling can be anything.

--
Bush crime family lost/embezzled $3 trillion from Pentagon.
Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
http://www.infowars.com/articles/us/mckinney_grills_rumsfeld.htm

Canadian Mind Products, Roedy Green.
See http://mindprod.com/iraq.html photos of Bush's war crimes
 
S

Stefan Schulz

Once you understand bit masks, the code is easier to read. If you
don't it is totally mysterious. It depends on your audience.

the bitmask code works with a number of stereotypical operations.
Individual bit fiddling can be anything.

Well, consider debugging the code. So variable flags is 2881219... what
does this tell me? Even if you see it in hex, you still have to look up
what each bit meant.
 
C

ChrisWSU

you can always add utility methods for debugging or readablity.
ie
boolean isFilterSet(){return __&filterFlag==0;}

void setFilter(){ ___|=filterFlag;}
 
H

Hal Rosser

- said:
Bitwise operators can be used to manage a set of boolean flags.
Would one bother to do so or is it simpler to just use separate flags?

some would bother, if he knew someone was going to look at the code, and he
wanted to impress them.
most would just use an array of boolean.
memory is not as precious as it used to be

even though you could <set> all the bits in one tick
You would still need to loop through the bits to check which ones were <on>
 
Y

Yamin

Well you have to decide what is important to you.

1. performance - using bitwise operators can save time in terms
copying the entire set. But separate boolean flags are quicker to
manipulate individually (which is how flags are normally done). But
this is probably not a core concern.

2. Ease of coding. This is a biggie and is really situation
dependent. Suppose you are decoding a network packet. There is a
32bit word that is a bunch of bit flags. Do you really want to sit
there are decode all 32 and then set individual boolean flags?
Especially if you only care about 1 or 2 of them, or maybe none of them
at all :) ?

3. Readability. I don't really see this as being too much of a
concern. if( value & BIT_MASK_1) is pretty readable. bitwise may
actually increase readability of things like copy
constructors/assignments :) 32 lines of copying for a normal boolean,
1 line for bitwise (assuming integer usage). You can very easily
mistype something in those 32 lines.

4 Memory usage / IO. This can add up. If you have a lot of
entities/devices each with their own boolean sets and you need to write
interval log files or even just keeping them in memory, it can add up
and may be of concern.
 
Y

Yamin

Well you have to decide what is important to you.

1. performance - using bitwise operators can save time in terms
copying the entire set. But separate boolean flags are quicker to
manipulate individually (which is how flags are normally done). But
this is probably not a core concern.

2. Ease of coding. This is a biggie and is really situation
dependent. Suppose you are decoding a network packet. There is a
32bit word that is a bunch of bit flags. Do you really want to sit
there are decode all 32 and then set individual boolean flags?
Especially if you only care about 1 or 2 of them, or maybe none of them
at all :) ?

3. Readability. I don't really see this as being too much of a
concern. if( value & BIT_MASK_1) is pretty readable. bitwise may
actually increase readability of things like copy
constructors/assignments :) 32 lines of copying for a normal boolean,
1 line for bitwise (assuming integer usage). You can very easily
mistype something in those 32 lines.

4 Memory usage / IO. This can add up. If you have a lot of
entities/devices each with their own boolean sets and you need to write
interval log files or even just keeping them in memory, it can add up
and may be of concern.

Yamin Bismilla
 
S

steve

Bitwise operators can be used to manage a set of boolean flags.
Would one bother to do so or is it simpler to just use separate flags?


in java one would not bother. it is too much trouble.
it is also difficult to maintain ,modify & expand
steve
 
D

Dale King

Yamin said:
3. Readability. I don't really see this as being too much of a
concern. if( value & BIT_MASK_1) is pretty readable.

But unfortunately in Java not very compilable. In Java that would have
to be:

if( ( value & BIT_MASK_1 ) != 0 )

which is a bit less readable.

For readability of such a thing, I miss the bitset type from Modula.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top