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


Groups > comp.lang.c > #11586

Re: unsigned == signed ?

From Quentin Pope <qp19433@hotmail.NOSPAM.com>
Newsgroups comp.lang.c
Subject Re: unsigned == signed ?
Date 2011-10-12 16:04 +0000
Organization Aioe.org NNTP Server
Message-ID <j74dr1$kus$1@speranza.aioe.org> (permalink)
References <j72687$bcn$1@speranza.aioe.org> <j72979$j2n$1@speranza.aioe.org> <lnlisrtdp6.fsf@nuthaus.mib.org>

Show all headers | View raw


On Tue, 11 Oct 2011 14:34:29 -0700, Keith Thompson wrote:

> Quentin Pope <qp19433@hotmail.NOSPAM.com> writes:
>> On Tue, 11 Oct 2011 19:43:03 +0000, Raj Pashwar 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?
>>
>> no ! unsigned int, and signed int are very different the way they are
>> represented in bits.
> 
> They're not all that different.  In fact, in the absence of padding bits
> and/or trap representations, the language guarantees that a value that's
> representable as int and unsigned int (i.e., in the range 0 .. INT_MAX)
> has the same representation in both types.
> 
>>                      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.
> 
> It's true that one of them is converted.  This is because the language
> standard says so; it's not a consequence of their representations.  The
> language *could* have defined "==" as a bitwise comparison (and in fact
> that would have yielded the same results for int vs. unsigned int
> comparisonms on 2's-complement systems).
> 
> But you're mistaken about the semantics of the conversion.  When an
> integer value is converted to an unsigned type, and the value cannot be
> represented in the target type, " the value is converted by repeatedly
> adding or subtracting one more than the maximum value that can be
> represented in the new type until the value is in the range of the new
> type" (C99 6.3.1.3p2).  In this particular case, convering the int value
> -1 to unsigned int yields UINT_MAX.
> 
>> 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)
> 
> It's more accurate to say that it's a *value* comparison operator; there
> needn't be any variables involved (consider "1 + 1 == 2").
> 
> The bitwise operators don't do comparisons.  If you want to do a bitwise
> equality comparison, the memcmp() function is probably the best
> approach.  Or you could treat the objects as arrays of unsigned char and
> compare the element values (that's basically what memcmp() does).
> 
>>> I also wanted to know if we can assign a number to uint & 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
> 
> 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) */
if (x == y)  printf("EQUAL");
will print 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