Unsigned/Signed Mismatch

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ioannis Vranos

    #16
    Re: Unsigned/Signed Mismatch

    "Andy Sawyer" <andys@despamme d.com> wrote in message
    news:ptkiszvi.f sf@evo6.com...[color=blue]
    >
    > Remember that char, signed char and unsigned char are three disticnt
    > types.[/color]



    char is implemented either as signed char or as unsigned char.



    FWIW, if I'm using the value in an arithmetic context, then I[color=blue]
    > always write either signed char or unsigned char. If I'm using it to
    > represent text elements, then I write char. And I probably wouldn't
    > write:
    >
    > char a = 3;
    >
    > I'd write
    >
    > char a = '\003';[/color]


    It's a personal issue. I am thinking in integral form, so i would write 3 if
    i had not a specific character in mind.


    [color=blue]
    > In fact, unsigned char may be promoted to unsigned int (depending on the
    > environment).[/color]



    If the involved values fit in int, they get promoted to int. It's in the
    standard.


    [color=blue]
    >[color=green]
    > > So in summary, a either signed char either unsigned gets converted to
    > > int.[/color]
    >
    > I had trouble parsing that - I assume you meant
    >
    > "So in summary, either a signed char or unsigned char gets converted to
    > int."
    > ?[/color]


    "So in summary the variable a, either it is signed char or unsigned char
    gets converted to int (in real world scenarios where sizeof(int)>1)" .






    --
    Ioannis

    * Programming pages: http://www.noicys.freeurl.com
    * Alternative URL 1: http://run.to/noicys
    * Alternative URL 2: http://www.noicys.cjb.net

    ---
    [ comp.std.c++ is moderated. To submit articles, try just posting with ]
    [ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.e du ]
    [ --- Please see the FAQ before posting. --- ]
    [ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]

    Comment

    • James Kanze

      #17
      Re: Unsigned/Signed Mismatch

      ivr@nothis.emai ls.ru ("Ioannis Vranos") writes:

      |> ""MiniDisc_2k2" " <MattDelB@nospa m.com> wrote in message
      |> news:3nmPa.50$z d4.42@lakeread0 2...

      |> > Well actually I was using chars only to save my mind from
      |> > calculating it all out. Let's try again.

      |> > I also intended this code to be used on a two's complement
      |> > machine.

      Which doesn't change anything here.

      |> > Use this:

      |> > signed int a = 3;
      |> > unsigned int b = -1; /* thus being the highest number represented by
      |> > unsigned int*/
      |> > if (b<a)

      |> > Now. If b were converted to a signed integer, b<a would be -1<3
      |> > == true. If a were converted to an unsigned integer, we would
      |> > get 65535 < 3 == false (in 16-bit environment). Which
      |> > representation does it follow. Does it proceed like the char
      |> > conversion, where it is converted to a signed int? Is this at
      |> > all standardized, or does the compiler get to pick?

      |> The C++ standard says:
      |>
      |> [conv.prom] 4.5 Integral promotions

      Wrong section. There are no promotions here.

      |> 1 An rvalue of type char, signed char, unsigned char, short int,
      |> or unsigned short int can be converted to an rvalue of type int if
      |> int can represent all the values of the source type; otherwise,
      |> the source rvalue can be converted to an rvalue of type unsigned
      |> int.

      [...]

      |> The (1) is your case.

      Not at all. That paragraph only applies if the type is char, signed
      char, unsigned char, short int or unsigned short int. There are none
      of those here.

      |> The answer is that everything is promoted to unsigned int and you
      |> must get a compiler warning about comparing signed with unsigned
      |> values.

      Right answer, wrong reason. See 5/9 -- it gives the complete list of
      conversions (call the usual arithmetic conversions) which apply to
      binary operators. In particular, the last point "Otherwise, if either
      operand is unsigned, the other shall be converted to unsigned."

      |> Try to compile and see.

      That's not always a valid solution. If he were comparing a long and
      an unsigned int, for example, the conversions used will depend on the
      implementation.

      --
      James Kanze mailto:kanze@ga bi-soft.fr
      Conseils en informatique orientée objet/
      Beratung in objektorientier ter Datenverarbeitu ng
      11 rue de Rambouillet, 78460 Chevreuse, France +33 1 41 89 80 93

      ---
      [ comp.std.c++ is moderated. To submit articles, try just posting with ]
      [ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.e du ]
      [ --- Please see the FAQ before posting. --- ]
      [ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]

      Comment

      • James Kanze

        #18
        Re: Unsigned/Signed Mismatch

        francis@robinto n.demon.co.uk (Francis Glassborow) writes:

        |> In article <%jjPa.31$zd4.2 @lakeread02>, MiniDisc_2k2
        |> <MattDelB@nospa m.com> writes
        |> >Okay, here's a question about the standard. What does it say about
        |> >unsigned/signed mismatches in a comparison statement:

        |> >char a = 3;
        |> >unsigned char b = 255;
        |> >if (a<b)

        |> >Now what's the real answer here? If a is converted to unsigned,
        |> >then b>a. But, if b is converted to signed,then a>b. What's the
        |> >correct coversion (what is the compiler supposed to do?)

        |> No, the normal integral promotions are applied first Which only
        |> becomes mildly interesting on systems where all integral types are
        |> the same size.

        But on such systems, 255 is within the range of char, so the problem
        isn't interesting either. (For that matter, as formulated, it isn't
        interesting on a system with 9 bit bytes either.)

        --
        James Kanze mailto:kanze@ga bi-soft.fr
        Conseils en informatique orientée objet/
        Beratung in objektorientier ter Datenverarbeitu ng
        11 rue de Rambouillet, 78460 Chevreuse, France +33 1 41 89 80 93

        ---
        [ comp.std.c++ is moderated. To submit articles, try just posting with ]
        [ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.e du ]
        [ --- Please see the FAQ before posting. --- ]
        [ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]

        Comment

        • Andy Sawyer

          #19
          Re: Unsigned/Signed Mismatch

          In article <3f0ef04e$0$242 $4d4eb98e@read. news.gr.uu.net> ,
          on Sat, 12 Jul 2003 00:16:53 +0000 (UTC),
          ivr@nothis.emai ls.ru ("Ioannis Vranos") wrote:
          [color=blue]
          > "Andy Sawyer" <andys@despamme d.com> wrote in message
          > news:ptkiszvi.f sf@evo6.com...[color=green]
          > >
          > > Remember that char, signed char and unsigned char are three disticnt
          > > types.[/color]
          >
          > char is implemented either as signed char or as unsigned char.[/color]

          Please cite your reference. I suggest you read 3.9.1p1, which says in part:

          ,----
          | Plain char, signed char, and unsigned char are three distinct types.
          `----

          It's in the standard (almost word-for-word - I omitted "plain").
          [color=blue][color=green]
          > > In fact, unsigned char may be promoted to unsigned int (depending on the
          > > environment).[/color]
          >
          > If the involved values fit in int, they get promoted to int. It's in the
          > standard.[/color]

          Again, cite your reference. I suggest you read 4.5p1, which says:

          ,----
          | An rvalue of type char, signed char, unsigned char, short int, or
          | unsigned short int can be converted to an rvalue of type int if int can
          | represent all the values of the source type; otherwise, the source
          | rvalue can be converted to an rvalue of type unsigned int.
          `----

          It's in the standard.

          Note the "if int can represent *ALL* values of the source type". (my
          emphasis). I suspect you're thinking of integral _conversions_, which
          (in this context) happen after promotions - see 5p9. It's in the standard.
          [color=blue][color=green][color=darkred]
          > > > So in summary, a either signed char either unsigned gets converted to
          > > > int.[/color]
          > >
          > > I had trouble parsing that - I assume you meant
          > >
          > > "So in summary, either a signed char or unsigned char gets converted to
          > > int."
          > > ?[/color]
          >
          >
          > "So in summary the variable a, either it is signed char or unsigned char
          > gets converted to int[/color]

          Not necessarily - it's in the standard.
          [color=blue]
          > (in real world scenarios where sizeof(int)>1)" .[/color]

          There are real world scenarios where sizeof(int)==si zeof(char). Neither
          your being unfamilair with them, nor the fact that they are less common
          than those where sizeof(int)>siz eof(char) does not in any way disprove
          their existence.

          Regards,
          Andy S.
          --
          "Light thinks it travels faster than anything but it is wrong. No matter
          how fast light travels it finds the darkness has always got there first,
          and is waiting for it." -- Terry Pratchett, Reaper Man

          ---
          [ comp.std.c++ is moderated. To submit articles, try just posting with ]
          [ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.e du ]
          [ --- Please see the FAQ before posting. --- ]
          [ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]

          Comment

          • Andrey Tarasevich

            #20
            Re: Unsigned/Signed Mismatch

            Ioannis Vranos wrote:[color=blue][color=green]
            >> ...
            >> Remember that char, signed char and unsigned char are three disticnt
            >> types.[/color]
            >
            > char is implemented either as signed char or as unsigned char.
            > ...[/color]

            Well, 'long int' can also be internally "implemente d" as 'int' (which is
            the case in many implementations ). Nevertheless, 'long int' and 'int'
            are two distinct types. The same applies to 'char', 'signed char' and
            'unsigned char'.

            --
            Best regards,
            Andrey Tarasevich
            Brainbench C and C++ Programming MVP

            Comment

            Working...