in reply to Flipping bits and using flags

I hope I'm not confused with youre update, but I think I may understand what you are asking. Say, for example, your bit flag is set to my $bit_flag = 0b101; #5. And, you need to find out if the given bit flag has a bit flipped within the list of qw( 1 2 4 8 ). If this is the case (and I apologize if I am still not understanding), you could:
#!/usr/bin/perl use strict; use warnings; my $bit_flag = 5; #0b101 my @bit_list = qw( 1 2 4 8 16 ); # Assuming you want to test for truth of my above statment # later in the application, binary 1/0 will work fine my $bit_flipped = ( grep{ $bit_flag & $_ } @bit_list ) ? 1 : 0; # Continue on with the application
Runing a bitwise and will simply tell you that there are some bits that are up in both the $bit_flat as within one of the values in the @bit_list.

CPAN has a good resource that might better explain what I am trying to say, while detailing the differences between the bitwise and (&) and the bitwise or (|).

Again, I hope I am understanding your question well enough to answer it. Good luck!

---hA||ta----
print$_ for(map{chr($_)}split(/\s+/,join(/\B?:\w+[^\s+]/,<DATA>))); __DATA__ 67 111 100 101 32 80 101 114 108

Replies are listed 'Best First'.
Re^2: Flipping bits and using flags
by BMaximus (Chaplain) on Mar 24, 2006 at 01:59 UTC
    Hey this is much appreciated but you missed the mark on this one unfortunately. In MySQL the sets can either be inserted by name or by inserting a number that corresponds to the numeric value given that element that is in the set. For example:

    In a table I have a field of type SET that is defined this way:

    "Dog","Cat","Bird","Reptile","Horse","Fish"

    MySQL allows you to either insert the set as one or many of the elements.

    A person says they have a Dog, Cat and a Fish. So it can be inserted as a list: 'Dog','Cat','Fish'

    The other way it can be inserted is by a number that represents the binary value. MySQL assigns a numberic value to each of the set's elements like so:

    "Dog" => 1, "Cat" => 2, "Bird" => 4, "Reptile" => 8, "Horse" => 16, "Fish" => 32

    Basically it's like a bit flag. MySQL allows up to 64 elements in a set. So if I were to insert by number I would add up the values for the elements. 1+2+4 = 7, when you insert 7, MySQL takes it as inserting 'Dog','Cat','Bird'. It's like using a binary flag.

    BMaximus