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


In reply to bit-wise boolean ops behave differently when observed by TomDLux

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.