can we compare two integers without using relational operators

R

raju

hi
can we compare two integers without using relational operators (== != <
<= > >=)

thanks
rajesh s
 
P

Pacher R. Dragos

raju said:
hi
can we compare two integers without using relational operators (== != <
<= > >=)

thanks
rajesh s

Maybe you want to compare their references in case of pointers but
those won't say you witch is the greater.
Or maybe you would like to make the comparision on bits with a mask?
Be more precise if you like precise answers.
 
K

Keith Thompson

raju said:
can we compare two integers without using relational operators (== != <
<= > >=)

We get this kind of question fairly frequently. It's roughly
equivalent to asking "Can I drive a screw without using a
screwdriver?" The answer is yes: you can use a hammer (but neither
the screw nor the wood is going to be in very good shape when you're
done), or you can use a pair of pliers and twist it in by hand (but
it's going to be *very* tedious). I'm sure there are other solutions.

The way to compare two integers is to use a relational operator.
That's what they're for.

Yes, there are probably ways to do comparisons without using
relational operators. If you can give us a good reason why you'd want
to do such a thing, we may be able to help. If it's just a
programming challenge with no actual practical application, some of us
might be interested enough to play along.

And if it's a homework assignment, you should do it yourself. If your
instructor wanted answers from Usenet rather than from his students,
he could have posted himself.
 
R

Richard Heathfield

Keith Thompson said:
And if it's a homework assignment, you should do it yourself. If your
instructor wanted answers from Usenet rather than from his students,
he could have posted himself.

....to where?
 
R

raju

Its just a programming challenge with no actual practical application.
i was just preparing for an interview and i came across this question.
i tried to do it but couldnot get the answer.
if this is wrong to post in this group, can you please help me where to
post my question.

thanks for your comments
rajesh s
 
L

lovecreatesbeauty

The following code you can take for a reference



/**************************************************
Author : ALNG
Date : 2003-03-11
Original : http://search.csdn.net/Expert/topic/1515/1515035.xml
**************************************************/

inline int signof(int i)
{
return unsigned(i) >> (sizeof (int) * 8 - 1);
}

int max(int a, int b)
{
int p[2];
p[0] = a;
p[1] = b;

return p[signof(a - b)];
}
 
Z

Zara

The following code you can take for a reference



/**************************************************
Author : ALNG
Date : 2003-03-11
Original : http://search.csdn.net/Expert/topic/1515/1515035.xml
**************************************************/

inline int signof(int i)
{
return unsigned(i) >> (sizeof (int) * 8 - 1);
}

int max(int a, int b)
{
int p[2];
p[0] = a;
p[1] = b;

return p[signof(a - b)];
}

Too hpeful a function, it works only on one'scomplement or two's
complement format, with sign bit being the most significant of all
bits and sizeof(char) being 8 bits. That is, it is pretty usual but
not 100% portable.

ha, ha, you will burn in hell for this!
 
M

mohansn

Too hpeful a function, it works only on one'scomplement or two's
complement format, with sign bit being the most significant of all
bits and sizeof(char) being 8 bits. That is, it is pretty usual but
not 100% portable.

ha, ha, you will burn in hell for this!

No offence meant but plz show me someone who uses anything other than
1' or 2's complement.
And sizeof(char) is 8 bits , sizeof(wchar_t) can be discussed.
 
S

Skarmander

And sizeof(char) is 8 bits , sizeof(wchar_t) can be discussed.

sizeof(char) is 1. The number of bits in a char is CHAR_BIT in
<limits.h>, which is at least 8. I know of course what you mean, but a
little precision in these matters pays off.

S.
 
C

CoL

you can use xor operator.Try this...

#include <stdio.h>

int main()
{

int a=10,b=10;

if(!(a^b))
printf("Numbers Are equal");

else
printf("Numbers Are Not equal");

}
 
F

Flash Gordon

Dik said:
Erm. CHAR_BIT of course.

and I've developed on systems with CHAR_BIT 16 (a char being 16 bits
wide) where sizeof(int)==1

I know of other systems with CHAR_BIT 24 and I believe I have heard of
ones where CHAR_BIT is 32.
 
F

Flash Gordon

raju said:
Its just a programming challenge with no actual practical application.
i was just preparing for an interview and i came across this question.
i tried to do it but couldnot get the answer.
if this is wrong to post in this group, can you please help me where to
post my question.

Please provide context when replying. Search the group for "google
context" for lots of discussion on this and instructions. Also complain
to google about their crappy interface.

As to your question, the request was for you to say why you wanted to do
it because:
1) The most obvious reason for asking such a question was a homework
assignment
2) Most of us cannot think of any good reason to avoid the tools
designed for the job.

For unsigned char (where there are no padding bits and representation is
guaranteed) you can implement it in various ways. The most obvious (but
probably not the fastest) being to implement long subtraction working
one bit at a time using masking to isolate the bits.
 
K

Kenny McCormack

Not to mention the fact that you haven't defined what you mean by
"compare" (although I think most of the responses have assumed you mean
"determine if equal or not" - note that other interpretations are possible).

Certainly one way to do it is:

void myCompare(int a,int b) {
printf("I know I'm probably biased, but I much prefer: %d to %d\n",a,b);
}
 
S

Skarmander

Kenny said:
Not to mention the fact that you haven't defined what you mean by
"compare" (although I think most of the responses have assumed you mean
"determine if equal or not" - note that other interpretations are possible).

Certainly one way to do it is:

void myCompare(int a,int b) {
printf("I know I'm probably biased, but I much prefer: %d to %d\n",a,b);
}

myCompare(5,5);
myCompare(1,2); myCompare(2,1);

Even with a deliberately whimsical interpretation, I doubt such results
would be acceptable... :)

S.
 
J

John Bode

raju said:
hi
can we compare two integers without using relational operators (== != <
<= > >=)

thanks
rajesh s

You can test for equality rather easily:

if (a - b)
printf ("a != b\n");
else
printf ("a == b\n");

For relational tests, you'd need to do some combination of subtraction
and bit shifting. For a two's complement machine, something like this
should work:

#define EQ(a,b) (!((a)-(b)))
#define CMP(a,b) (((a)-(b)) >> sizeof a - 1)
#define LT(a,b) (!EQ((a),(b)) & CMP((a),(b)))
#define LE(a,b) (EQ((a),(b)) | LT((a),(b))))
#define GT(a,b) (!EQ((a),(b)) & !CMP((a),(b)))
#define GE(a,b) (EQ((a),(b)) | GT((a),(b)))

I've never had to work with 1's complement architectures, so I don't
know how this would work on them (not very well, I'd imagine).
 
K

Keith Thompson

John Bode said:
You can test for equality rather easily:

if (a - b)
printf ("a != b\n");
else
printf ("a == b\n");

Not reliably. If a and b are signed, an overflow (e.g., if a==INT_MAX
and b==INT_MIN) invokes undefined behavior. It's likely to "work" on
many systems, but you shouldn't rely on it.
 
W

Walter Roberson

You can test for equality rather easily:
if (a - b)
printf ("a != b\n");
else
printf ("a == b\n");

If a and b happen to be IEEE NaN, then a - b is going to be NaN.
You then compare that NaN to 0, but all comparisons of NaN to
anything (including themselves) are false. Your program would conclude
that the two numbers are equal, when they might be entirely different
NaNs.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top