471,056 Members | 1,652 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

unsigned == signed ?

HH
int main()
{
uint x = -1;
int y = -1;
if (x == y)
printf("EQUAL");
else
printf("NOT EQUAL");
}
This code prints "EQUAL". Does this mean that bit comparison is done for the
equality test? If it doesn't can you give me an example that shows bit
comparison isn't done? I also wanted to know if we can assign a number to
uint & int variables such that "unit var != int var". Thanks so much
Nov 14 '05 #1
7 1660
"HH" <HH@earthlink.net> wrote in
news:1c******************@newsread1.news.pas.earth link.net:
int main()
{
uint x = -1;
int y = -1;
if (x == y)
printf("EQUAL");
else
printf("NOT EQUAL");
}
This code prints "EQUAL". Does this mean that bit comparison is done for
the equality test?
I would say an unsigned comparison is being done. I believe that y will be
promoted by C to unsigned int (which I assume uint is typedef'd to be)
before doing the comparison so they should be equal. You should have
gotten a warning that you are comparing signed with unsiged types - a clue
to not do that.
If it doesn't can you give me an example that shows
bit comparison isn't done? I also wanted to know if we can assign a
number to uint & int variables such that "unit var != int var". Thanks
so much


To compare values you must use reasonable types, there is no such thing as
an unsiged -1 so how can you expect a meaningful comparison. You can
always compare int to int and unsigned to unsigned but when comparing
unsigned to signed it simply may not make sense.

--
- Mark ->
--
Nov 14 '05 #2
Greetings,

HH wrote:
int main()
{
uint x = -1;
int y = -1;
if (x == y)
printf("EQUAL");
else
printf("NOT EQUAL");
}
This code prints "EQUAL". Does this mean that bit comparison is done for the
equality test? If it doesn't can you give me an example that shows bit
comparison isn't done? I also wanted to know if we can assign a number to
uint & int variables such that "unit var != int var". Thanks so much


Someone will likely complain about the lack of type uint in C as well as
the undefined behaviour caused by not including stdio.h, as well as the
possible lack of output due to not newline at the end of the printf().

The answer, however, assuming uint to be equivalent to unsigned int, is
that y is promoted to unsigned int before the comparison courtesy of
section 6.3.1.8 : usual arithmetic conversion and these two values are
indeed equal.
--
Kyle A. York
Sr. Subordinate Grunt
DSBU

Nov 14 '05 #3
HH wrote:
int main()
{
uint x = -1;
int y = -1;
if (x == y)
printf("EQUAL");
else
printf("NOT EQUAL");
}
This code prints "EQUAL".
Well, no: This code doesn't compile, because `uint'
has not been declared. (There are other problems, too,
but that's not the point.) I'll answer on the assumption
that `uint' is a typedef'ed or #define'd alias for
`unsigned int'.
Does this mean that bit comparison is done for the
equality test? If it doesn't can you give me an example that shows bit
comparison isn't done?
No and no. C's comparison operators are all defined
as working with the values of their operands, not the
representations thereof. What you have discovered is
that (1) when an `unsigned int' and an `int' are compared,
the `int' value is first converted to `unsigned int', and
(2) `(unsigned int)-1 == (unsigned int)-1'.
I also wanted to know if we can assign a number to
uint & int variables such that "unit var != int var". Thanks so much


Assuming that `unit' is also an alias for `unsigned
int', it is possible for this to happen -- but only under
a rather "exotic" set of conditions. These conditions are
permitted by the C language Standard, but never ("What,
never?" "Well, hardly ever.") seen in practice:

1) The number must be in the allowable range for an
`unsigned int' but out of range for an `int', that
is, `INT_MAX < number && number <= UINT_MAX'.

2) The attempt to convert this too-large value to an
`int' produces an implementation-defined result
instead of raising an implementation-defined signal.

3) The implementation-defined result is a valid `int'
value with the interesting property that converting
it to `unsigned int' does not yield the original
number.

This chain of events is unlikely, but permitted by the
Standard -- converting a value from `unsigned int' to `int'
and back again is required to be the identity operation only
if the value is in the legitimate range of an `int'. If it's
not, the Standard permits other things to happen. It's more
a theoretical possibility than anything else, though.

Here's the big question: Why do you care? What are you
trying to do?

--
Er*********@sun.com

Nov 14 '05 #4

"HH" <HH@earthlink.net> wrote in message
news:1c******************@newsread1.news.pas.earth link.net...
int main()
{
uint x = -1;
int y = -1;
if (x == y)
printf("EQUAL");
else
printf("NOT EQUAL");
}
This code prints "EQUAL". Does this mean that bit comparison is done for the equality test? If it doesn't can you give me an example that shows bit
comparison isn't done? no ! unsigned int, and signed int are very different the way they are
represented in bits. therefore, when you to compare the 2 different types
(like comparing apples and oranges) one of them has to be converted, in this
case, the 'int' is converted to a 'uint', and becomes 0, since unsigned
numbers cannot be less then zero.
and besides, the == operator it *not* a bit comparison operator, it is a
variable comparison operator, if you want to do bit comparison you use the
bitwise operators, &,|,^,~. (and, or, exclusive or, not. respectively)
I also wanted to know if we can assign a number to
unit & int variables such that "unit var != int var". Thanks so much yes, if unit var = 1, and int var is any value other than 1 (-1, 0, -10, 10,
100, whatever) then they will be 'not equal' if you use the operator '!='
for example:

uint x = 1;
int y = -1;
if (x == y)
printf("EQUAL");
else
printf("NOT EQUAL");

will print NOT EQUAL since y is converted to an unsigned int and becomes 0

Nov 14 '05 #5
On 6 Aug 2004 15:52:38 GMT, "Mark A. Odell" <od*******@hotmail.com>
wrote in comp.lang.c:
"HH" <HH@earthlink.net> wrote in
news:1c******************@newsread1.news.pas.earth link.net:
int main()
{
uint x = -1;
int y = -1;
if (x == y)
printf("EQUAL");
else
printf("NOT EQUAL");
}
This code prints "EQUAL". Does this mean that bit comparison is done for
the equality test?


I would say an unsigned comparison is being done. I believe that y will be
promoted by C to unsigned int (which I assume uint is typedef'd to be)
before doing the comparison so they should be equal. You should have
gotten a warning that you are comparing signed with unsiged types - a clue
to not do that.
If it doesn't can you give me an example that shows
bit comparison isn't done? I also wanted to know if we can assign a
number to uint & int variables such that "unit var != int var". Thanks
so much


To compare values you must use reasonable types, there is no such thing as
an unsiged -1 so how can you expect a meaningful comparison. You can
always compare int to int and unsigned to unsigned but when comparing
unsigned to signed it simply may not make sense.


Beg to disagree. There is indeed such a thing as unsigned -1. C
defines both initializations and assignments in terms of values.

Initializing any unsigned integer type with the value of -1, or
assigning a value of -1 to any unsigned integer type, follows the
usual unsigned rules of modulo wrap around.

UTYPE x = -1;

....is guaranteed to produce the value UTYPE_MAX in x.

Given:

int si = -1;
unsigned int ui = -1;

....then the comparison:

if (si == ui)

....is guaranteed to be true. If the implementation uses a non-2's
complement representation for negative signed integer types, the bit
patterns of ui and si will be different, but the comparison is also
based on *value*, not on bit representation.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #6
On Sat, 7 Aug 2004 08:27:30 +0800, "someone else"
<zo****@fuck.the.spammers.net> wrote in comp.lang.c:

"HH" <HH@earthlink.net> wrote in message
news:1c******************@newsread1.news.pas.earth link.net...
int main()
{
uint x = -1;
int y = -1;
if (x == y)
printf("EQUAL");
else
printf("NOT EQUAL");
}
This code prints "EQUAL". Does this mean that bit comparison is done for the
equality test? If it doesn't can you give me an example that shows bit
comparison isn't done?

no ! unsigned int, and signed int are very different the way they are
represented in bits. therefore, when you to compare the 2 different types
(like comparing apples and oranges) one of them has to be converted, in this
case, the 'int' is converted to a 'uint', and becomes 0, since unsigned
numbers cannot be less then zero.


This is totally incorrect. Assigning or initializing any unsigned
integer type with the value -1 is required and guaranteed to produce
the maximum value of the unsigned type. Indeed no unsigned type can
hold the value -1, but the conversion is completely and specifically
defined.
and besides, the == operator it *not* a bit comparison operator, it is a
variable comparison operator, if you want to do bit comparison you use the
bitwise operators, &,|,^,~. (and, or, exclusive or, not. respectively)
I also wanted to know if we can assign a number to
unit & int variables such that "unit var != int var". Thanks so much

yes, if unit var = 1, and int var is any value other than 1 (-1, 0, -10, 10,
100, whatever) then they will be 'not equal' if you use the operator '!='
for example:


You completely misunderstood this part of the OP's question.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #7

"Jack Klein" <ja*******@spamcop.net> wrote in message
news:1k********************************@4ax.com...
On Sat, 7 Aug 2004 08:27:30 +0800, "someone else"
<zo****@fuck.the.spammers.net> wrote in comp.lang.c:

"HH" <HH@earthlink.net> wrote in message
news:1c******************@newsread1.news.pas.earth link.net...
int main()
{
uint x = -1;
int y = -1;
if (x == y)
printf("EQUAL");
else
printf("NOT EQUAL");
}
This code prints "EQUAL". Does this mean that bit comparison is done
for the
equality test? If it doesn't can you give me an example that shows bit
comparison isn't done? no ! unsigned int, and signed int are very different the way they are
represented in bits. therefore, when you to compare the 2 different types (like comparing apples and oranges) one of them has to be converted, in this case, the 'int' is converted to a 'uint', and becomes 0, since unsigned
numbers cannot be less then zero.


This is totally incorrect. Assigning or initializing any unsigned
integer type with the value -1 is required and guaranteed to produce
the maximum value of the unsigned type. Indeed no unsigned type can
hold the value -1, but the conversion is completely and specifically
defined.

true, my mistake.
however the values are converted first. to prove it, try this
long x = -1; /* (32 bit, signed) */
unit y = -1; /* (16 bit, unsigned) */
if (x == y) printf("EQUAL");
will print EQUAL !
and besides, the == operator it *not* a bit comparison operator, it is a
variable comparison operator, if you want to do bit comparison you use the bitwise operators, &,|,^,~. (and, or, exclusive or, not. respectively)
I also wanted to know if we can assign a number to
unit & int variables such that "unit var != int var". Thanks so much yes, if unit var = 1, and int var is any value other than 1 (-1, 0, -10, 10, 100, whatever) then they will be 'not equal' if you use the operator '!=' for example:


You completely misunderstood this part of the OP's question.

again, you are correct.
but it's too bad that my personal beliefs do not allow me to respond... (I
oppose rudeness)
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html

Nov 14 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Siemel Naran | last post: by
4 posts views Thread by techie | last post: by
3 posts views Thread by Nicholas Zhou | last post: by
10 posts views Thread by =?iso-8859-2?B?SmFuIFJpbmdvuQ==?= | last post: by
6 posts views Thread by Kislay | last post: by

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.