Re: compare a large number of variables



On Thu, 18 Aug 2005 17:11:25 +0000, Michael Wojcik wrote:
> In article <pan.2005.08.17.14.30.11.267556@xxxxxxxxxxx>, Netocrat
> <netocrat@xxxxxxxxxxx> writes:
>> On Tue, 16 Aug 2005 23:55:15 -0700, Einar wrote:
>>
>> > Yes, your suggestions work perfectly, but I was mor looking for a
>> > nice bit operator to operate on all my variables and then to compare
>> > it with the value. something like (a|b|c|d...z) != value (this wont
>> > work however... ).
>>
>> How about (off the top of my head and untested):
>>
>> if ( !( ((a|b|c|d...z) == value) && ((a&b&c&d...z) == value) ) )
>> /* one or more of a,b,c,d...z is not equal to value */
>
> Or the similarly silly:
>
> if (a^value|b^value|c^value|...|z^value)
> /* one or more of a,b,c,...,z is not equal to value */

No that doesn't work. In the case of one variable with a==1, value==1
then a^value is false. I can't see a way to rearrange it using bitwise
or/and to work.

This really calls for a multi-way equality comparison:

if ( ! (a == b == c == d == ... == z == value) )

Obviously the semantics are wrong the way the equality operator is
currently defined, but I've sometimes wanted to use an expression like
this.

> (Untested, but I think that's right.) Or similarly:
>
> if (a^b|b^c|c^d|...|y^z|z^value)

Same problem as above.

> All assuming that we're working with values that aren't going to produce
> trap representations, or with unsigned types.

Right, these bitwise operations on signed integers are not portable.

> Of course this is just a bitwise version of (in the second case):
>
> if (a!=b || b!=c || ... || z!=value)

Except that the logical or can't be replaced with a bitwise or.

[...]
>> I don't think that would generally be as efficient as using multiple
>> comparisons against value because in that case the first mismatch will
>> prevent the rest from being evaluated.
>
> More important, it's lousy code,

Its purpose is not immediately clear and it depends on unsigned integers.
Both issues can be dealt with by appropriate commenting. More typical
expressions would avoid the need for commenting but I don't think that
makes it "lousy" code - it's perfectly serviceable, just a little atypical
and obscure.

> and performance is rarely as
> important as maintainability. And if performance *is* crucial in this
> case, then it's probably time to redesign - it's unlikely that keeping a
> lot of independent variables and testing them all against a single value
> is actually the best way to accomplish whatever it is that the problem
> requires.

That's quite a sweeping claim (as to likelihood of needing a multi-way
test) and I haven't given much thought to its merits but I'm curious by
what reasoning you make it.

[...]

--
http://members.dodo.com.au/~netocrat

.



Relevant Pages

  • Re: compare a large number of variables
    ... >> bit operator to operate on all my variables and then to compare it with ... Of course this is just a bitwise version of: ... More important, it's lousy code, and performance is rarely as ... logical operators, not the bitwise ones), as long as it was clear. ...
    (comp.lang.c)
  • Bitwise (flags) enums: how to compare?
    ... I tried hard to find on google some reference about bitwise (flags) ... What's the proper way to define and compare bitwise enums? ...
    (comp.lang.java.programmer)
  • Re: question about binary and serial info
    ... Sounds like you want the bitwise and operator, ... > i am using the serial module to read data from a serial input. ... > have to compare these things with AND. ... i want to know so i dont get halfway into ...
    (comp.lang.python)
  • Re: comparing the result of two comparisions
    ... See previous post for the trick with Bitwise AND. ... > I can't find a way to compare the results of two comparisions. ... > this is just another comparision of 2 boolean values, but somehow Sql ...
    (microsoft.public.sqlserver.programming)