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


Groups > comp.lang.c > #11617

Re: unsigned == signed ?

From Phil Carmody <thefatphil_demunged@yahoo.co.uk>
Newsgroups comp.lang.c
Subject Re: unsigned == signed ?
Date 2011-10-13 14:54 +0300
Organization A noiseless patient Spider
Message-ID <8739ext8d2.fsf@bazspaz.fatphil.org> (permalink)
References <j72687$bcn$1@speranza.aioe.org> <j72979$j2n$1@speranza.aioe.org> <0.5aec1a9d7a573e9ac1bf.20111011223559BST.87vcrvmcsg.fsf@bsb.me.uk>

Show all headers | View raw


Ben Bacarisse <ben.usenet@bsb.me.uk> writes:
> 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. 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.
> 
> Not zero, but UINT_MAX.

He looks like he's under the misapprehension that the conversion is a
saturating one (anything that goes out of bounds gets mapped onto the 
value just inside the bound that was exceeded.

So I think the better correction is the mathematical expression 
UINT_MAX+1+x
Which for x==-1 is UINT_MAX, as you say.
But for x==2 it's UINT_MAX-1, etc.

> > 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 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 
> 
> See above!

Yup, I'm sure he thinks it saturates.

Phil
-- 
"Religion is what keeps the poor from murdering the rich."
-- Napoleon

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