Velocity Reviews > unsigned == signed ?

# unsigned == signed ?

HH
Guest
Posts: n/a

 08-06-2004
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

Mark A. Odell
Guest
Posts: n/a

 08-06-2004
"HH" <(E-Mail Removed)> wrote in

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

kyle york
Guest
Posts: n/a

 08-06-2004
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

Eric Sosman
Guest
Posts: n/a

 08-06-2004
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?

--
http://www.velocityreviews.com/forums/(E-Mail Removed)

someone else
Guest
Posts: n/a

 08-07-2004

"HH" <(E-Mail Removed)> wrote in message
> 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

>
>

Jack Klein
Guest
Posts: n/a

 08-07-2004
On 6 Aug 2004 15:52:38 GMT, "Mark A. Odell" <(E-Mail Removed)>
wrote in comp.lang.c:

> "HH" <(E-Mail Removed)> wrote in
>
> > 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

Jack Klein
Guest
Posts: n/a

 08-07-2004
On Sat, 7 Aug 2004 08:27:30 +0800, "someone else"
<zooloo@****.the.spammers.net> wrote in comp.lang.c:

>
> "HH" <(E-Mail Removed)> wrote in message
> > 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

someone else
Guest
Posts: n/a

 08-08-2004

"Jack Klein" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On Sat, 7 Aug 2004 08:27:30 +0800, "someone else"
> <zooloo@****.the.spammers.net> wrote in comp.lang.c:
>
> >
> > "HH" <(E-Mail Removed)> wrote in message
> > > 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

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post pozz C Programming 12 03-20-2011 11:32 PM Rob1bureau VHDL 1 02-27-2010 12:13 AM kyrpa83 VHDL 1 10-17-2007 06:58 PM Patrick VHDL 1 06-07-2004 01:59 PM Jeremy Pyle VHDL 3 06-28-2003 10:47 PM