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


Groups > comp.lang.c > #11545

Re: unsigned == signed ?

From James Kuyper <jameskuyper@verizon.net>
Newsgroups comp.lang.c
Subject Re: unsigned == signed ?
Date 2011-10-11 16:23 -0400
Organization A noiseless patient Spider
Message-ID <4E94A5B1.8080303@verizon.net> (permalink)
References <j72687$bcn$1@speranza.aioe.org>

Show all headers | View raw


On 10/11/2011 03:43 PM, Raj Pashwar wrote:
> int main()
> {
> uint x = -1;

uint is presumably a typedef; it's not a typedef provided by the C
standard itself, so you should have let us know what type it is a
typedef for. I'll presume "unsigned int".

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

No, it means that precisely the same conversion is performed on -1 when
it is used to initialize x, as is performed on the value of y when it is
converted to the type of 'x' for the comparison. Assuming that uint is a
typedef for unsigned int, the result is guaranteed to be UINT_MAX in
both cases.

> ... If it doesn't can you give me an example that shows bit
> comparison isn't done?

Yes. Change 'y' to signed char, and x to unsigned long. If sizeof(signed
char) < sizeof(unsigned long) (which is almost, but not quite, certainly
guaranteed), most of the bits that are set in x won't have any
comparable bits in 'y'. However, the values will still compare equal,
because y's value of -1 will be converted to ULONG_MAX before the
conversion.

In the unlikely event that you can locate an implementation where 'int'
uses a 1's complement or sign-magnitude representation, you'll find that
you still get exactly the same result, which ALSO proves that it's a
value comparison, not a bit comparison. That's because x and y will have
very different bit patterns on such a system.

> ... I also wanted to know if we can assign a number to
> uint & int variables such that "unit var != int var". Thanks so much 

Not as long as the number is within the range [INT_MIN, INT_MAX].
Because the same conversions are performed in both cases, the values
necessarily compare equal.

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