TomDLux has asked for the wisdom of the Perl Monks concerning the following question:
My application involves data storted in a database, one field of which is a bitmapped value.
The user-defined routines provided by one of our team members uses these values correctly, to process the records in various ways.
But some queries need to look up a bit-field, alter it, and use the result in a new query.
SQL doesn't do bit-fields, so I have to do it in Perl.
I do something like this ..
$bitfield = .... my @fields = ( $modifier =~ m{ ([+-]) (\d+) }gxms ); # extract mods while ( @fields >= 2 ) { # for each modification my $op = shift @fields; my $value = shift @fields; if ( $op eq '+') { $bitfield |= $value; # set a bit w/ OR } elsif ($op eq '-' ) { my $dummy; # no-op to force full-width $dummy = sprintf "%064b\n", $value; my $negated = ~$value; $trade_condition &= $negated # clear a bit w/ negated AND } }
I put in the sprintf because it cleared up a problem that negating one produced zero.
But I still have a problem that starting off with a $bitfield value of one and modifying it with $modifier = '+2+16+1024' produces 3562 when I'm not looking, or 1043 when I single-step through it. Actually, single-stepping isn't enough, I need to be observing. Using
> printf "064b\n", $bitfieldmakes it come out right.
How can I make bit boolean ops reliable ... or is it a matter of raising my intelligence to stop making foolish mistakes? PS ... This is perl, v5.8.8 built for i686-linux-thread-multi--
TTTATCGGTCGTTATATAGATGTTTGCA
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: bit-wise boolean ops behave differently when observed
by kyle (Abbot) on Jan 18, 2008 at 21:08 UTC | |
|
Re: bit-wise boolean ops behave differently when observed
by TomDLux (Vicar) on Jan 18, 2008 at 21:40 UTC |