Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #11593

Re: unsigned == signed ?

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: unsigned == signed ?
Date 2011-10-12 10:49 -0700
Organization None to speak of
Message-ID <lnhb3et808.fsf@nuthaus.mib.org> (permalink)
References <j72687$bcn$1@speranza.aioe.org> <j72979$j2n$1@speranza.aioe.org> <lnlisrtdp6.fsf@nuthaus.mib.org> <j74dr1$kus$1@speranza.aioe.org>

Show all headers | View raw


Quentin Pope <qp19433@hotmail.NOSPAM.com> writes:
> On Tue, 11 Oct 2011 14:34:29 -0700, Keith Thompson wrote:
>> Quentin Pope <qp19433@hotmail.NOSPAM.com> writes:
[...]
>>> 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
>> 
>> No, it will print NOT EQUAL because y is converted to an unsigned int
>> and becomes UINT_MAX (typically 4294967295), which is not equal to 1.
>> You'd get the same result if "==" did a bitwise comparison.
>
> 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) */

What makes you think that long is 32 bits and uint (unsigned int?) is
16 bits?  They're *at least* those widths, but they could be wider.
(I suspect that int is actually 32 bits on your system.)

> if (x == y)  printf("EQUAL");
> will print EQUAL ! 

Not if long is wider than int, as you assert.  (More precisely, not
if long can represent all the values of unsigned int.)  On such
a system, y will be initialized to UINT_MAX, which is 65535.
In the comparison, the "usual arithmetic conversions" will promote
the value of y from unsigned int to (signed) long, resulting in
an equality comparison between 65535 and -1, both of type long.
It will not print "EQUAL".

On the other hand, if int and long are both 32 bits, then both x
and y are converted to unsigned long, so the comparison is between
ULONG_MAX and ULONG_MAX; in that case, it will print "EQUAL".

And why do you insist on using the name "uint"?  We've said before
that there is no such predefined type in C.  If you mean "unsigned
int", please say "unsigned int".  If you really want to call it
"uint", provide a typedef.  (For all we know, "uint" could be a
typedef for _Bool.)

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

unsigned == signed ? Raj Pashwar <raj121190@hotmail.NOSPAM.com> - 2011-10-11 19:43 +0000
  Re: unsigned == signed ? Keith Thompson <kst-u@mib.org> - 2011-10-11 12:57 -0700
  Re: unsigned == signed ? James Kuyper <jameskuyper@verizon.net> - 2011-10-11 16:23 -0400
  Re: unsigned == signed ? Quentin Pope <qp19433@hotmail.NOSPAM.com> - 2011-10-11 20:33 +0000
    Re: unsigned == signed ? James Kuyper <jameskuyper@verizon.net> - 2011-10-11 16:40 -0400
    Re: unsigned == signed ? Keith Thompson <kst-u@mib.org> - 2011-10-11 14:34 -0700
      Re: unsigned == signed ? Quentin Pope <qp19433@hotmail.NOSPAM.com> - 2011-10-12 16:04 +0000
        Re: unsigned == signed ? Keith Thompson <kst-u@mib.org> - 2011-10-12 10:49 -0700
        Re: unsigned == signed ? pete <pfiland@mindspring.com> - 2011-10-12 21:12 -0400
    Re: unsigned == signed ? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-10-11 22:35 +0100
      Re: unsigned == signed ? Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2011-10-13 14:54 +0300
        Re: unsigned == signed ? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-10-13 15:26 +0100
    Re: unsigned == signed ? "J. J. Farrell" <jjf@bcs.org.uk> - 2011-10-12 03:02 +0100
  Re: unsigned == signed ? Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2011-10-13 13:56 +0300

csiph-web