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", $bitfield

makes 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

    Try this:

    my $bitfield = 1; my $modifier = '+2+16+1024'; 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 } } print "bf: $bitfield\n"; __END__ bf: 1043

    That's the desired result, right? If I change the first line:

    my $bitfield = '1';

    ...then I get 3624. This isn't the result you got (3562), but I still think the problem is that you're starting out with a string somewhere, or you're using strings where you think you have numbers. This way...

    my $bitfield = '1'; $bitfield = 0+$bitfield;

    ...the result goes back to 1043.

Re: bit-wise boolean ops behave differently when observed
by TomDLux (Vicar) on Jan 18, 2008 at 21:40 UTC

    That did it!

    Can I kiss you, Kyle?

    I replaced the $dummy stuff with

    my $value = ... $value += 0; # number, not a string
    That cleard up the
     Argument "M-N" isn't numeric in bitwise and (&)
    error message.

    --
    TTTATCGGTCGTTATATAGATGTTTGCA