unsigned == signed ?

H

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
 
M

Mark A. Odell

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

kyle york

Greetings,
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.
 
E

Eric Sosman

HH said:
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?
 
S

someone else

HH said:
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
 
J

Jack Klein

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.


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

Jack Klein

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)

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

someone else

Jack Klein said:
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 !
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)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top