in reply to comparison with bitwise operators

Your code as written is very clear and will also run very fast. A fewer number of characters in the source code does not necessarily translate into more efficient run-time code. Clarity should be a major objective of code.

In short, what you have written is fine and I would leave it that way.

Replies are listed 'Best First'.
Re^2: comparison with bitwise operators
by LanX (Saint) on Jan 14, 2011 at 02:06 UTC
    When checking against just two values you are certainly right, but for more values the other approaches scale better.

    Cheers Rolf

      I think we are in agreement.
      I was keying on the OP's statement of the problem: "compare a value to two numbers".
      The proposed formulation is very, very good! I see no reason to "improve" it.

      For efficiency, with a much larger set of possibilities, "does value X exist within a data set?", I would think that the List::MoreUtils qw (any); solution is the best for two reasons:
      a)Since it is an XS module it will run faster than native Perl code,
      b)the grep solution will examine all possibilities in order to return the scalar result of number of "matches" whereas a well written XS module would return true immediately upon the first match.

      The Perl "or" code will also "give up" and stop processing immediately upon the first successful match (OR condition is satisfied). I think this is just fine for say 1/2 dozen "or" conditions. Of course the expected probability of returning "true" should be taken into account in the ordering of an "if" statement. This may result in faster code than using say the "smart match" operator in Perl 5.10. Benchmark the program if there is any doubt.

        Agreed, just that I'd prefer the hash solution in cases where scalability matters.

        (Furthermore List::MoreUtils isn't core.)

        Cheers Rolf

        UPDATE: 3 downvotes???