Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > compare a large number of variables

Reply
Thread Tools

compare a large number of variables

 
 
Einar
Guest
Posts: n/a
 
      08-16-2005
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.

 
Reply With Quote
 
 
 
 
Peteris Krumins
Guest
Posts: n/a
 
      08-16-2005

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

 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      08-16-2005


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.

--


 
Reply With Quote
 
Peteris Krumins
Guest
Posts: n/a
 
      08-16-2005

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

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      08-16-2005
"Einar" <> 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- <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.
 
Reply With Quote
 
Jack Klein
Guest
Posts: n/a
 
      08-17-2005
On 16 Aug 2005 08:12:35 -0700, "Einar" <> 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
 
Reply With Quote
 
Einar
Guest
Posts: n/a
 
      08-17-2005
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.

 
Reply With Quote
 
AM
Guest
Posts: n/a
 
      08-17-2005
Hi,

This might help you...

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

/* file.c */
/* Inside your function */
#define USE(var) do { \
if ( ##var != value) \
{ \
/* your stuff */ \
} \
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

 
Reply With Quote
 
Netocrat
Guest
Posts: n/a
 
      08-17-2005
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.


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

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

 
Reply With Quote
 
freak@Infectedmail.com
Guest
Posts: n/a
 
      08-17-2005
Rerwite code to have them all grouped in a user defined type and then
use memcmp().

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Compare two extremely large lists? Joe Young Perl Misc 7 01-18-2011 11:22 AM
Put variables into member variables or function variables? tjumail@gmail.com C++ 9 03-23-2008 04:03 PM
How to compare two large text files? www Java 3 06-29-2007 02:03 PM
OT: Number Nine, Number Nine, Number Nine FrisbeeŽ MCSE 37 09-26-2005 04:06 PM
How to compare these variables by use operator 'AND' pattanawadee C Programming 5 09-23-2003 04:36 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57