R
raju
hi
can we compare two integers without using relational operators (== != <
<= > >=)
thanks
rajesh s
can we compare two integers without using relational operators (== != <
<= > >=)
thanks
rajesh s
raju said:hi
can we compare two integers without using relational operators (== != <
<= > >=)
thanks
rajesh s
raju said:hi
can we compare two integers without using relational operators (== != <
<= > >=)
raju said:can we compare two integers without using relational operators (== != <
<= > >=)
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.
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!
And sizeof(char) is 8 bits , sizeof(wchar_t) can be discussed.
> And sizeof(char) is 8 bits , sizeof(wchar_t) can be discussed.
>
> No offense, but sizeof(char) = BITSPERBYTE bits.
Dik said:Erm. CHAR_BIT of course.
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.
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);
}
raju said:hi
can we compare two integers without using relational operators (== != <
<= > >=)
thanks
rajesh s
John Bode said:You can test for equality rather easily:
if (a - b)
printf ("a != b\n");
else
printf ("a == b\n");
You can test for equality rather easily:
if (a - b)
printf ("a != b\n");
else
printf ("a == b\n");
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.