can we compare two integers without using relational operators

C

Christian Bau

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

Of course. Consider the integers 0 and 1. 0 looks like an oval, while 1
looks like a long line plus a short line at a 45 degree angle at the
top. You can take this as an example to compare other integers. Just for
fun, compare 6 and 9.
 
W

Walter Roberson

Of course. Consider the integers 0 and 1. 0 looks like an oval, while 1
looks like a long line plus a short line at a 45 degree angle at the
top. You can take this as an example to compare other integers. Just for
fun, compare 6 and 9.

Those aren't the integers you are comparing, just representations of them.
 
S

Skarmander

Walter said:
Those aren't the integers you are comparing, just representations of them.

I was shouting that to my compiler just this instant. But it didn't
care. The bastard.

S.
 
N

Niklas Norrthon

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

thanks
rajesh s

Untested:

int is_equal(int x, int y)
{
return !! (((unsigned int) x) ^ ((unsigned int) y));
}
 
C

Chris McDonald

Did we ever determine *why* one would want to do this?
Is it just a point of curiosity, or another contrived homework problem,
like not using sizeof() ?
 
C

Christopher Benson-Manica

Chris McDonald said:
Did we ever determine *why* one would want to do this?
Is it just a point of curiosity, or another contrived homework problem,
like not using sizeof() ?

OP stated that he came across the question while preparing for an
interview, and that it did not have any practical application.
 
K

Keith Thompson

Chris McDonald said:
Did we ever determine *why* one would want to do this?
Is it just a point of curiosity, or another contrived homework problem,
like not using sizeof() ?

Yes, the original poster came back and wrote:

] 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.

in message <[email protected]>.
 
C

Chris McDonald

Yes, the original poster came back and wrote:
] 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;
I shouldn't have come in on the thread late.
 
C

Christian Bau

Christopher Benson-Manica said:
OP stated that he came across the question while preparing for an
interview, and that it did not have any practical application.

In an interview, the appropriate answer would be that there is most
likely some way to do it, but it would be pointless and unreadable code,
and therefore not of any interest to a C programmer.

If that doesn't satisfy the interviewer, then you might not get the job,
but I would call that a close escape :)
 
K

Keith Thompson

Christian Bau said:
In an interview, the appropriate answer would be that there is most
likely some way to do it, but it would be pointless and unreadable code,
and therefore not of any interest to a C programmer.

If that doesn't satisfy the interviewer, then you might not get the job,
but I would call that a close escape :)

Not necessarily. The question could be designed to test the
candidate's ability to use his imagination and do C programming that
goes beyond the obvious. There are real-world problems of comparable
difficulty; the virtue of *this* particular question is that it's easy
to state the problem.

If I were interviewing someone, I would expect them both to tell me
that the question has little or no real-world significance (the
relational operators exist for a reason) *and* to be able to come up
with at least a partial solution.
 
K

Kenny McCormack

Keith Thompson said:
Not necessarily. The question could be designed to test the
candidate's ability to use his imagination and do C programming that
goes beyond the obvious. There are real-world problems of comparable
difficulty; the virtue of *this* particular question is that it's easy
to state the problem.

C programming that goes beyond the obvious is off-topic here.
 
P

Peter Nilsson

Christian said:
In an interview, the appropriate answer would be that there is most
likely some way to do it, but it would be pointless and unreadable code,
and therefore not of any interest to a C programmer.

It would be of interest to some C programmers... [from the a previous
time when the same question came up...]
>
> One answer to that can be fun and recreational programming. And
> by exploring the language with unusual contraints you can sometimes
> learn something new and surprising about the language and what it
> can do.

Of course, I doubt Lawrence would ever need to justify his C skills in
an interview. ;)
 
K

Keith Thompson

C programming that goes beyond the obvious is off-topic here.

That's complete nonsense, and you know it. It's not even particularly
amusing.

Kenny, in the past you have been a self-acknowledged deliberate troll.
More recently, you seem to have reformed a bit. I've even seen you
post something useful in the last day or two.

You have a choice. You can try to participate in this newsgroup in a
sensible manner, perhaps even making a significant contribution, or
you can continue acting like a fool and be ignored.
 
P

Peter Nilsson

Kenny said:
C programming that goes beyond the obvious is off-topic here.

Perhaps you've been mislead by the volume of trivial but off-topic
Windows and Unix specific questions that get asked in clc, and
subsequently get redirected to more appropriate forums.

But those questions don't mean that non-trivial C questions don't
exist, nor does it mean that they are not asked. And it certainly
doesn't mean they are not welcome. Indeed, the regulars often ask
questions or call for code reviews of non-trivial code.
 
B

Ben Pfaff

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

I don't think that anyone else has yet pointed out that == and !=
are not relational operators, so there it is. (They are equality
operators.)
 
K

Kenny McCormack

That's complete nonsense, and you know it. It's not even particularly
amusing.

Kenny, in the past you have been a self-acknowledged deliberate troll.
More recently, you seem to have reformed a bit. I've even seen you
post something useful in the last day or two.

IOW, bashing the newbies for their OT posts is good sport (clearly so, and
I'd never say otherwise), but bashing a regular for the same thing is
(channeling the Fashion Club) "not done".

OK...
 
K

Keith Thompson

IOW, bashing the newbies for their OT posts is good sport (clearly so, and
I'd never say otherwise), but bashing a regular for the same thing is
(channeling the Fashion Club) "not done".

OK...

Not at all.

"Bashing" newbies for off-topic posts would be inappropriate, unless
the newbies in question are being persistently obtuse. I don't claim
it never happens, but it's not nearly as common as you'd like us to
believe.

Telling newbies that their posts are off-topic, and (usually) giving
them good advice about where to go to get their question answered is
useful and kind, both to them and to the rest of us.

Deliberate trolling, especially by someone who has been around long
enough to know better, is a major reason for the existence of
killfiles.

And this followup is almost certainly a waste of my time. It's only
because of the "almost" that I bother, but I probably won't again.
 
R

Richard Bos

Christopher Benson-Manica said:
OP stated that he came across the question while preparing for an
interview, and that it did not have any practical application.

Of course, should this question really come up in a job interview, the
correct answer would be along the lines of "I'm sorry, but I don't want
to work here any more. I'd rather mow lawns than write that kind of
code. Goodbye, Mr. Ballmer."

Richard
 
N

Niklas Norrthon

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.

My current computer is a DS9001 ultra turbo, with the compiler ecc (evil c compiler)
version 11.7.8.5.1.3.

sizeof (char) = 1
CHAR_BITS = 11

The rightmost bit is a signbit, and the other 10 bits forms the value. "Negative Zero"
is the trap value of course.

1 is encoded as 00000 00001 0
-1 as 00000 00001 1

and so on...

(could the above conform to the standard?)

/Niklas Norrthon
 

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

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,061
Latest member
KetonaraKeto

Latest Threads

Top