Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: What A Wonderful World: Bitmasks!

by dws (Chancellor)
on Dec 08, 2002 at 08:41 UTC ( [id://218352]=note: print w/replies, xml ) Need Help??


in reply to What A Wonderful World: Bitmasks!

At a conceptual level, packaging a set of boolean values into a bitmask does seem to be equivalent to packaging them in a hash, and at an implementation level, it does seem like a way to save space. But I think it's a dangerous way to proceed, for several reasons:

1. It's too easy to do hard-to-detect damage other bits as a side-effect of making a change to one bit.

2. The steps you might take to avoid collateral damage have the effect of burdening the application. Compare

$args{FOO) = 1; $args{BAR} = 0;
to
setbit($args, FOO}; clearbit($args, BAR);

3. Method signatures (such as they are in Perl) lose expressive power when arguments are packed together. It gets harder to get a sense of what a chunk of code is doing when you can't quickly and easily tell what a subroutine or method is taking as input (or producing as output). Conceptually, you're widening the method signature to include all of the values packed in to the bit pack, whether they're used or not.

Replies are listed 'Best First'.
Re^2: What A Wonderful World: Bitmasks!
by Aristotle (Chancellor) on Dec 10, 2002 at 16:03 UTC
    4. It's nowhere near as easy for the caller to dynamically construct a bitmask as it is to dynamically construct a list of booleans. You will spend a lot of time doing things like
    my $param = 0; $param |= FOO if $foo > 0; $param |= BAR if $bar != 1; $param |= BAZ if $baz > $foo; quux($param);
    where you would otherwise just write quux($foo > 0, $bar != 1, $baz > $foo); Ok, it might work (I didn't test, might need an extra dozen parens) to say something like quux( ($foo > 0 && FOO) | ($bar != 1 && BAR) | ($baz > $foo && BAZ) ); but I don't think I need to comment on that.

    Makeshifts last the longest.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://218352]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2024-04-26 08:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found