Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   compare a large number of variables (http://www.velocityreviews.com/forums/t439095-compare-a-large-number-of-variables.html)

 Einar 08-16-2005 03:12 PM

compare a large number of variables

Hi,

I wonder if there is a nice bit twiddling hack to compare a large
number of variables?

If you first store them in an array, you can do:

for (i = 0; i < n; i++) {
if (array[i] != value) {
/* array[i] differs from value, do something*/
}
}

but I dont have the variables in an array, and would like to figure out
a nice oneliner.

best regards.
E.

 Peteris Krumins 08-16-2005 03:28 PM

Re: compare a large number of variables

Einar wrote:
> Hi,
>
> I wonder if there is a nice bit twiddling hack to compare a large
> number of variables?
>
> If you first store them in an array, you can do:
>
> for (i = 0; i < n; i++) {
> if (array[i] != value) {
> /* array[i] differs from value, do something*/
> }
> }
>
> but I dont have the variables in an array, and would like to figure out
> a nice oneliner.
>

N nice one liners:

if (var1 != value) { /* do smth */ }
if (var2 != value) { /* do smth */ }
....
if (varN != value) { /* do smth */ }

P.Krumins

 Eric Sosman 08-16-2005 03:32 PM

Re: compare a large number of variables

Einar wrote:
> Hi,
>
> I wonder if there is a nice bit twiddling hack to compare a large
> number of variables?
>
> If you first store them in an array, you can do:
>
> for (i = 0; i < n; i++) {
> if (array[i] != value) {
> /* array[i] differs from value, do something*/
> }
> }
>
> but I dont have the variables in an array, and would like to figure out
> a nice oneliner.

I'm not sure what you're looking for, but it might
be one of

if (a != value || b != value || ... || z != value) {
/* at least one of a,b,...,z differs from
* value, do something */
}

or

if (a != value && b != value && ... &&& z != value) {
/* all of a,b,...,z differ from value, do
* something */
}

or

if (a != value) {
/* a differs from value, do something */
}
if (b != value) {
/* b differs from value, do something */
}
...
if (z != value) {
/* z differs from value, do something */
}

or as above but with `else' before all but the first `if'

or

int *ptr[] = { &a, &b, ..., &z };
for (i = 0; i < n; ++i) {
if (*ptr[i] != value) {
/* The i'th of a,b,...,z differs from
* value, do something */
}
}

If none of these is what you're trying to do, you'll
have to explain your intent more clearly.

--
Eric.Sosman@sun.com

 Peteris Krumins 08-16-2005 04:02 PM

Re: compare a large number of variables

Einar wrote:
> Hi,
>
> I wonder if there is a nice bit twiddling hack to compare a large
> number of variables?
>
> If you first store them in an array, you can do:
>
> for (i = 0; i < n; i++) {
> if (array[i] != value) {
> /* array[i] differs from value, do something*/
> }
> }
>
> but I dont have the variables in an array, and would like to figure out
> a nice oneliner.
>

N nice one liners:

if (var1 != value) { /* do smth */ }
if (var2 != value) { /* do smth */ }
....
if (varN != value) { /* do smth */ }

P.Krumins

 Keith Thompson 08-16-2005 07:52 PM

Re: compare a large number of variables

"Einar" <orneinar@yahoo.se> writes:
> I wonder if there is a nice bit twiddling hack to compare a large
> number of variables?
>
> If you first store them in an array, you can do:
>
> for (i = 0; i < n; i++) {
> if (array[i] != value) {
> /* array[i] differs from value, do something*/
> }
> }
>
> but I dont have the variables in an array, and would like to figure out
> a nice oneliner.

Perhaps they should be in an array.

If that's not an option, and you're going to be doing this a lot, you
might consider setting up an array of pointers to the variables and
looping over that.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.

 Jack Klein 08-17-2005 03:08 AM

Re: compare a large number of variables

On 16 Aug 2005 08:12:35 -0700, "Einar" <orneinar@yahoo.se> wrote in
comp.lang.c:

> Hi,
>
> I wonder if there is a nice bit twiddling hack to compare a large
> number of variables?
>
> If you first store them in an array, you can do:
>
> for (i = 0; i < n; i++) {
> if (array[i] != value) {
> /* array[i] differs from value, do something*/
> }
> }
>
> but I dont have the variables in an array, and would like to figure out
> a nice oneliner.
>
> best regards.
> E.

Here is a "trick" that I do not really recommend as best practice, but
I have used in small microcontrollers for embedded systems with severe
memory constraints.

Assuming you have a header that contains the following:

my_vars.h

extern int val_a;
extern int val_b;
/* ... */
extern int val_y;
extern int val_z;

....and of course you have a source file:

my_vars.c

int val_a;
int val_b;
/* ... */
int val_y;
int val_z;

....then you could change my_vars.h to:

extern int sneaky_array[];
#define val_a sneaky_array[0]
#define val_b sneaky_array[1]
/* ... */
#define val_y sneaky_array[24]
#define val_z sneaky_array[25]

....and change my_vars.c:

int sneaky_array[2];

....then you can write new code that can loop through the values as an
array, whereas older source code that refers to individual elements by
name will still compile and work with the new my_vars.h header.

But I really, really don't recommend this except for exceptional
circumstances.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html

 Einar 08-17-2005 06:55 AM

Re: compare a large number of variables

Hello Eric,

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... ). This is just to get rid of a for-loop or lots of
if-statements, so it is nothing necesary, I'm just convinced that it is
possible to solve in another way, and I can't forget about it....
Ahhhrg.

best regards.
E.

 AM 08-17-2005 02:28 PM

Re: compare a large number of variables

Hi,

/* file.e */
USE(a)
USE(b)
USE(c)

/* file.c */
#define USE(var) do { \
if ( ##var != value) \
{ \
} \
while(0);
#include "file.e"

you can compile the code with -E option (gcc) and see the expanded code
as follows-

do { if (a > value ) { } while (0);
do { if (b > value ) { } while (0);
do { if (c > value ) { } while (0);

Regards,
-A M

 Netocrat 08-17-2005 02:30 PM

Re: compare a large number of variables

On Tue, 16 Aug 2005 23:55:15 -0700, Einar wrote:

> Hello Eric,
>
> 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... ). This is just to get rid of a for-loop or lots of
> if-statements, so it is nothing necesary, I'm just convinced that it is
> possible to solve in another way, and I can't forget about it....
> Ahhhrg.

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 */

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. You never know though - on some
hardware it might be; or the compiler might rewrite it for you.

Another alternative (also untested):

if ( ((a|b|c|d...z)&a&b&c&d...z) != value )
/* one or more of a,b,c,d...z is not equal to value */

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

 freak@Infectedmail.com 08-17-2005 06:41 PM

Re: compare a large number of variables

Rerwite code to have them all grouped in a user defined type and then
use memcmp().

All times are GMT. The time now is 11:41 AM.