hi
can we compare two integers without using relational operators (== != <
<= > >=)
thanks
rajesh s 49 14818
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.
raju wrote: hi can we compare two integers without using relational operators (== != < <= > >=)
yes.
"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.
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)
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
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)];
}
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!
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. 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.
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/
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/
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
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.
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.
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);
}
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.
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).
"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.
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
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.
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
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.
"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));
}
>"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.
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.
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.
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.
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 :-)
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.
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.
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 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.
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
"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
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... 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.
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
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)
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. 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
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)
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.
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
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.
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. 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.
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
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". This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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) {...
|
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:
...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: Aliciasmith |
last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
|
by: giovanniandrean |
last post by:
The energy model is structured as follows and uses excel sheets to give input data:
1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
|
by: NeoPa |
last post by:
Hello everyone.
I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report).
I know it can be done by selecting :...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM)
Please note that the UK and Europe revert to winter time on...
|
by: nia12 |
last post by:
Hi there,
I am very new to Access so apologies if any of this is obvious/not clear.
I am creating a data collection tool for health care employees to complete. It consists of a number of...
|
by: isladogs |
last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, Mike...
|
by: GKJR |
last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
| |