Re: compare a large number of variables
- From: Netocrat <netocrat@xxxxxxxxxxx>
- Date: Fri, 19 Aug 2005 04:20:47 +1000
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
.
- Follow-Ups:
- Re: compare a large number of variables
- From: Michael Wojcik
- Re: compare a large number of variables
- From: Tim Rentsch
- Re: compare a large number of variables
- From: Mark
- Re: compare a large number of variables
- From: pete
- Re: compare a large number of variables
- References:
- compare a large number of variables
- From: Einar
- Re: compare a large number of variables
- From: Eric Sosman
- Re: compare a large number of variables
- From: Einar
- Re: compare a large number of variables
- From: Netocrat
- Re: compare a large number of variables
- From: Michael Wojcik
- compare a large number of variables
- Prev by Date: Re: RPG fight engine.
- Next by Date: getting variable length strings from stdin
- Previous by thread: Re: compare a large number of variables
- Next by thread: Re: compare a large number of variables
- Index(es):
Relevant Pages
|