473,382 Members | 1,332 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

can we compare two integers without using relational operators

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

thanks
rajesh s

Nov 15 '05 #1
49 14842

raju wrote:
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.

Nov 15 '05 #2
raju wrote:
hi
can we compare two integers without using relational operators (== != <
<= > >=)


yes.
Nov 15 '05 #3
"raju" <ra********@gmail.com> writes:
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <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.
Nov 15 '05 #4
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?

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/2005
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Nov 15 '05 #5
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

Nov 15 '05 #6
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)];
}

Nov 15 '05 #7
On 24 Oct 2005 02:48:29 -0700, "lovecreatesbeauty"
<lo***************@gmail.com> wrote:
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!
Nov 15 '05 #8

Zara wrote:
On 24 Oct 2005 02:48:29 -0700, "lovecreatesbeauty"
<lo***************@gmail.com> wrote:
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
************************************************* */

<snip>

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.

Nov 15 '05 #9
mo*****@gmail.com wrote:
<snip>
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.
Nov 15 '05 #10
In article <11*********************@f14g2000cwb.googlegroups. com> mo*****@gmail.com writes:
....
And sizeof(char) is 8 bits , sizeof(wchar_t) can be discussed.


No offense, but sizeof(char) = BITSPERBYTE bits.
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Nov 15 '05 #11
In article <Io********@cwi.nl> "Dik T. Winter" <Di********@cwi.nl> writes:
In article <11*********************@f14g2000cwb.googlegroups. com> mo*****@gmail.com writes:
...
> And sizeof(char) is 8 bits , sizeof(wchar_t) can be discussed.


No offense, but sizeof(char) = BITSPERBYTE bits.


Erm. CHAR_BIT of course.
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Nov 15 '05 #12
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");

}

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

thanks
rajesh s


Nov 15 '05 #13
Dik T. Winter wrote:
In article <Io********@cwi.nl> "Dik T. Winter" <Di********@cwi.nl> writes:
> In article <11*********************@f14g2000cwb.googlegroups. com> mo*****@gmail.com writes:
> ...
> > And sizeof(char) is 8 bits , sizeof(wchar_t) can be discussed.

>
> No offense, but sizeof(char) = BITSPERBYTE bits.


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.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Nov 15 '05 #14
raju 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.


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.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Nov 15 '05 #15
In article <11**********************@g14g2000cwa.googlegroups .com>,
raju wrote:
hi
can we compare two integers without using relational operators (== != <
<= > >=)

thanks
rajesh s


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);
}

Nov 15 '05 #16
Kenny McCormack wrote:
In article <11**********************@g14g2000cwa.googlegroups .com>,
raju wrote:
hi
can we compare two integers without using relational operators (== != <
<= > >=)

thanks
rajesh s


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.
Nov 15 '05 #17

raju wrote:
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).

Nov 15 '05 #18
"John Bode" <jo*******@my-deja.com> writes:
raju wrote:
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");


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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <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.
Nov 15 '05 #19
In article <11**********************@g43g2000cwa.googlegroups .com>,
John Bode <jo*******@my-deja.com> wrote:
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.
--
"It is important to remember that when it comes to law, computers
never make copies, only human beings make copies. Computers are given
commands, not permission. Only people can be given permission."
-- Brad Templeton
Nov 15 '05 #20
In article <11**********************@g47g2000cwa.googlegroups .com>,
"raju" <ra********@gmail.com> wrote:
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.
Nov 15 '05 #21
In article <ch*********************************@slb-newsm1.svr.pol.co.uk>,
Christian Bau <ch***********@cbau.freeserve.co.uk> wrote:
In article <11**********************@g47g2000cwa.googlegroups .com>,
"raju" <ra********@gmail.com> wrote:
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.


Those aren't the integers you are comparing, just representations of them.
--
Many food scientists have reported chocolate to be the single most
craved food. -- Northwestern University, 2001
Nov 15 '05 #22
Walter Roberson wrote:
In article <ch*********************************@slb-newsm1.svr.pol.co.uk>,
Christian Bau <ch***********@cbau.freeserve.co.uk> wrote:
In article <11**********************@g47g2000cwa.googlegroups .com>,
"raju" <ra********@gmail.com> wrote:


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.

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.

Nov 15 '05 #23
raju wrote:
hi
can we compare two integers without using relational operators (== != <
<= > >=)


http://groups.google.com/group/comp....eee18e55ea156c

--
Peter

Nov 15 '05 #24
"raju" <ra********@gmail.com> writes:
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));
}

Nov 15 '05 #25
>"raju" <ra********@gmail.com> writes:
hi
can we compare two integers without using relational operators (== != <
<= > >=)

thanks
rajesh s

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() ?

--
Chris.
Nov 15 '05 #26
Chris McDonald <ch***@csse.uwa.edu.au> wrote:
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.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 15 '05 #27
Chris McDonald <ch***@csse.uwa.edu.au> writes:
"raju" <ra********@gmail.com> writes:
hi
can we compare two integers without using relational operators (== != <
<= > >=)

thanks
rajesh s


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 <11**********************@o13g2000cwo.googlegroups .com>.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <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.
Nov 15 '05 #28
Keith Thompson <ks***@mib.org> writes:
Chris McDonald <ch***@csse.uwa.edu.au> writes:
"raju" <ra********@gmail.com> writes:
hi
can we compare two integers without using relational operators (== != <
<= > >=)

thanks
rajesh s
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.

Thanks;
I shouldn't have come in on the thread late.

--
Chris.
Nov 15 '05 #29
In article <dj**********@chessie.cirr.com>,
Christopher Benson-Manica <at***@nospam.cyberspace.org> wrote:
Chris McDonald <ch***@csse.uwa.edu.au> wrote:
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.


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 :-)
Nov 15 '05 #30
Christian Bau <ch***********@cbau.freeserve.co.uk> writes:
In article <dj**********@chessie.cirr.com>,
Christopher Benson-Manica <at***@nospam.cyberspace.org> wrote:
Chris McDonald <ch***@csse.uwa.edu.au> wrote:
> 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.


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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <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.
Nov 15 '05 #31
In article <ln************@nuthaus.mib.org>,
Keith Thompson <ks***@mib.org> wrote:
....
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.

Nov 15 '05 #32
Christian Bau wrote:
Christopher Benson-Manica <at***@nospam.cyberspace.org> wrote:
Chris McDonald <ch***@csse.uwa.edu.au> wrote:
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.


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

Lawrence Kirby wrote: Mike Wahler wrote:
... why would you want to avoid using a language feature
specifically designed to do what you asked about (comparing
values). ...


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. ;)

--
Peter

Nov 15 '05 #33
ga*****@yin.interaccess.com (Kenny McCormack) writes:
In article <ln************@nuthaus.mib.org>,
Keith Thompson <ks***@mib.org> wrote:
...
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.


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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <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.
Nov 15 '05 #34
Kenny McCormack wrote:
In article <ln************@nuthaus.mib.org>,
Keith Thompson <ks***@mib.org> wrote:
...
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.


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.

--
Peter

Nov 15 '05 #35
"raju" <ra********@gmail.com> writes:
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.)
--
"The expression isn't unclear *at all* and only an expert could actually
have doubts about it"
--Dan Pop
Nov 15 '05 #36
In article <ln************@nuthaus.mib.org>,
Keith Thompson <ks***@mib.org> wrote:
ga*****@yin.interaccess.com (Kenny McCormack) writes:
In article <ln************@nuthaus.mib.org>,
Keith Thompson <ks***@mib.org> wrote:
...
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.


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

Nov 15 '05 #37
ga*****@yin.interaccess.com (Kenny McCormack) writes:
In article <ln************@nuthaus.mib.org>,
Keith Thompson <ks***@mib.org> wrote:
ga*****@yin.interaccess.com (Kenny McCormack) writes:
In article <ln************@nuthaus.mib.org>,
Keith Thompson <ks***@mib.org> wrote:
...
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.


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


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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <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.
Nov 15 '05 #38
Christopher Benson-Manica <at***@nospam.cyberspace.org> wrote:
Chris McDonald <ch***@csse.uwa.edu.au> wrote:
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.


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
Nov 15 '05 #39
Zara said:
return unsigned(i) >> (sizeof (int) * 8 - 1);


Apart from the other comments, there is no function called 'unsigned' in C.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/2005
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Nov 15 '05 #40
On 2005-10-26, Richard Bos <rl*@hoekstra-uitgeverij.nl> wrote:
Christopher Benson-Manica <at***@nospam.cyberspace.org> wrote:
Chris McDonald <ch***@csse.uwa.edu.au> wrote:
> 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.


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


But then he would F*ing Kill You [tm], He Has Done It Before And He
Will Do It Again - all kidding aside, it's an interview question,
not a serious proposed program design.
Nov 15 '05 #41
mo*****@gmail.com writes:
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
Nov 15 '05 #42
On 2005-10-26, Niklas Norrthon <do********@invalid.net> wrote:
mo*****@gmail.com writes:
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


No. -1 would have to be represented as any of the three:

10000000001 signed-magnitude
11111111110 ones complement
11111111111 twos complement

In general, the sign bit has to be at the 'left' (i.e MSB for unsigned)
Nov 15 '05 #43
Ben Pfaff wrote:
"raju" <ra********@gmail.com> writes:

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


That's a nice gotcha. I see the standard defines them in exactly this
way. Mathematically, of course, equality and inequality are
relationships. The standard itself acknowledges this in 7.12.14, while
maintaining the terminology distinction.

Interestingly, the standard slips up in Appendix F: heading F.8.3 reads
"relational operators", but != and == are discussed. This should have
been "relational and comparison operators" for full consistency.

"Comparison operators" seems to be a reasonable name for both equality
operators and relational operators. Though, of course, this is all
nitpicking and nobody will be confused as long as you're spelling out
the operators, which will almost always be the case.

S.
Nov 15 '05 #44
Jordan Abel <jm****@purdue.edu> wrote:
On 2005-10-26, Richard Bos <rl*@hoekstra-uitgeverij.nl> wrote:
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."


But then he would F*ing Kill You [tm], He Has Done It Before And He
Will Do It Again - all kidding aside, it's an interview question,
not a serious proposed program design.


Yes, and all kidding aside, if a prospective employer thinks that this
is a question worth asking in a job interview I would think twice about
accepting a job there, because I would apparently have to work under a
management that does not understand what programming is about.

Richard
Nov 15 '05 #45
Jordan Abel wrote:
On 2005-10-26, Niklas Norrthon <do********@invalid.net> wrote:
mo*****@gmail.com writes:

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

No. -1 would have to be represented as any of the three:

10000000001 signed-magnitude
11111111110 ones complement
11111111111 twos complement


In C89, sign-(magnitude - 1) also is possible as any true binary
representation is allowed, so
10000000000
is in the game as well, as may be others.
C99 indeed restricts it to the three above.

Cheers
Michael
In general, the sign bit has to be at the 'left' (i.e MSB for unsigned)


--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Nov 15 '05 #46
On 2005-10-26, Michael Mair <Mi**********@invalid.invalid> wrote:
Jordan Abel wrote:
On 2005-10-26, Niklas Norrthon <do********@invalid.net> wrote:
mo*****@gmail.com writes:
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

No. -1 would have to be represented as any of the three:

10000000001 signed-magnitude
11111111110 ones complement
11111111111 twos complement


In C89, sign-(magnitude - 1) also is possible as any true binary
representation is allowed, so
10000000000
is in the game as well, as may be others.
C99 indeed restricts it to the three above.


On looking at "c88" [which is what i've decided to call the c89
draft that's floating around], it appears that nothing is specified
at all (the actual standard may specify, but the draft does not)
except for the presence of evidence that none of the committee had
even conceived of the possibility that the sign might be elsewhere
than at the left.

so in theory it could be something oddball like 11000000000 - the
only thing that appears to be specified about negative numbers is
that the MSB is 1.
Nov 15 '05 #47
rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
Jordan Abel <jm****@purdue.edu> wrote:
On 2005-10-26, Richard Bos <rl*@hoekstra-uitgeverij.nl> wrote:
> 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."


But then he would F*ing Kill You [tm], He Has Done It Before And He
Will Do It Again - all kidding aside, it's an interview question,
not a serious proposed program design.


Yes, and all kidding aside, if a prospective employer thinks that this
is a question worth asking in a job interview I would think twice about
accepting a job there, because I would apparently have to work under a
management that does not understand what programming is about.


I disagree. Obviously if I were asked to do such a thing in a real
program, I'd have serious questions. But it's a reasonable test of a
candidate's problem-solving ability. A good candidate should be able
both to explain that it's a stupid thing to do in real code *and* to
come up with at least a partial solution.

Any problem-solving exercise that can be stated briefly but that
doesn't have a simple solution is likely to be contrived. Someone who
can figure this one out is more likely to be able to solve real-world
problems of comparable difficulty that are too complex to go over in a
job interview.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <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.
Nov 15 '05 #48
On Wed, 26 Oct 2005 09:43:28 +0000 (UTC), Jordan Abel
<jm****@purdue.edu> wrote:
On 2005-10-26, Niklas Norrthon <do********@invalid.net> wrote: <snip>
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. <snip> (could the above conform to the standard?)

No. -1 would have to be represented as any of the three:

10000000001 signed-magnitude
11111111110 ones complement
11111111111 twos complement

In general, the sign bit has to be at the 'left' (i.e MSB for unsigned)


Yes and no. All value (magnitude) bits of the signed representation
must must have the same weights in unsigned so that all positive
signed numbers have the same representation as unsigned. The bit that
is the sign in signed may go unused (padding) in unsigned. If it is
used it must be _more_ significant than the 'common' value bits; it
still need not be _most_ significant if there are padding bits unused
in signed but used for value in unsigned, but that seems perverse.

- David.Thompson1 at worldnet.att.net
Nov 15 '05 #49
On 2005-11-07, Dave Thompson <da*************@worldnet.att.net> wrote:
On Wed, 26 Oct 2005 09:43:28 +0000 (UTC), Jordan Abel
<jm****@purdue.edu> wrote:
On 2005-10-26, Niklas Norrthon <do********@invalid.net> wrote:

<snip>
> 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. <snip> > (could the above conform to the standard?)

No. -1 would have to be represented as any of the three:

10000000001 signed-magnitude
11111111110 ones complement
11111111111 twos complement

In general, the sign bit has to be at the 'left' (i.e MSB for unsigned)


Yes and no. All value (magnitude) bits of the signed representation
must must have the same weights in unsigned so that all positive
signed numbers have the same representation as unsigned. The bit that
is the sign in signed may go unused (padding) in unsigned. If it is
used it must be _more_ significant than the 'common' value bits; it
still need not be _most_ significant if there are padding bits unused
in signed but used for value in unsigned, but that seems perverse.

- David.Thompson1 at worldnet.att.net


The c89 standard, or at least the draft i have a copy of, is not
explicit about signed representation, but anywhere it talks about a bit
which may be a sign bit, it says "the high-order bit" - which tells me
that by 1988 nobody had thought of the possibility that signed values
might be represented in a way that had the sign bit anywhere other than
"the high-order bit".
Nov 15 '05 #50

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

87
by: j0mbolar | last post by:
I've read in the standard that addresses basically can't be interpreted as integers. If they can, it is implementation defined behavior. However, if they can't be viewed as integers in any sense...
122
by: Einar | last post by:
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 != value) {...
14
by: AliceB.Toklas | last post by:
In my Absolute Beginner's Guide to C book by Greg Perry where it is instruction how relational operators work it gives the following example: int i = 5; so the following statement is true: ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.