By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,831 Members | 1,031 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,831 IT Pros & Developers. It's quick & easy.

replacement for relational operations

P: n/a
hello to all,

Any one please tell me how to compare two variables or constants without using
relational operations.

thank you in advance,
vishnu.
Nov 13 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
vishnu mahendra <vi*************@yahoo.com> scribbled the following:
hello to all, Any one please tell me how to compare two variables or constants without using
relational operations.


Well, if they're both numbers, substract them from each other and use
the result as an if expression. The if will be false if they're equal
or true if they're not. If you want to know which is bigger, ask a
higher C guru. I don't really know all that much about bit-twiddling
arithmetic.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"I am not very happy acting pleased whenever prominent scientists overmagnify
intellectual enlightenment."
- Anon
Nov 13 '05 #2

P: n/a
On 1 Dec 2003 17:37:41 GMT, in comp.lang.c , Joona I Palaste
<pa*****@cc.helsinki.fi> wrote:
vishnu mahendra <vi*************@yahoo.com> scribbled the following:
hello to all,

Any one please tell me how to compare two variables or constants without using
relational operations.


Well, if they're both numbers, substract them from each other and use
the result as an if expression. The if will be false if they're equal
or true if they're not. If you want to know which is bigger, ask a
higher C guru. I don't really know all that much about bit-twiddling
arithmetic.


how about this, for unsigned types only

if(!(a-b)) puts("the same");
else
{
while(a-- && b--) a--, b--;
if(a && !b) puts ("a was larger");
if(!a && b) puts ("b was larger");
}

you can probably trivially extend this to cover negative numbers by
operating on combinations of a and b together.

You could also to do something like this:
if(b-a)
{
errno = 0;
log(b-a);
if(errno) puts("b was smaller than a");
errno = 0;
log(a-b);
if(errno) puts("a was smaller than b");
}
else
puts ("a equals b")
although that assumes neither a=b nor b-a is outside the range of a
double, that you don't get an underflow in the log, and that
math_errhandling ORs with MATH_ERRNO. I'm sure some cleverness could
work round all this.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 13 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.