Initialize int and char with maximum values

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Der Andere

    Initialize int and char with maximum values

    I want to initialize unsigned int and unsigned char with their maximum
    value.
    Just for being sure: Does
    unsigned int i = pow(256,sizeof( i))-1;
    unsigned char c = pow(256,sizeof( c))-1;
    always work?

    Thanks,
    Matthias

    --
    Für emails Anweisung in der Adresse befolgen


  • Peter van Merkerk

    #2
    Re: Initialize int and char with maximum values

    Der Andere wrote:[color=blue]
    > I want to initialize unsigned int and unsigned char with their maximum
    > value.
    > Just for being sure: Does
    > unsigned int i = pow(256,sizeof( i))-1;
    > unsigned char c = pow(256,sizeof( c))-1;
    > always work?[/color]

    Use the std::numeric_li mits class instead:

    #include <limits>

    int main()
    {
    unsigned char c = std::numeric_li mits<unsigned char>::max();
    unsigned int i = std::numeric_li mits<unsigned int>::max();
    return 0;
    }

    --
    Peter van Merkerk
    peter.van.merke rk(at)dse.nl


    Comment

    • Der Andere

      #3
      Re: Initialize int and char with maximum values

      > > I want to initialize unsigned int and unsigned char with their maximum[color=blue][color=green]
      > > value.
      > > Just for being sure: Does
      > > unsigned int i = pow(256,sizeof( i))-1;
      > > unsigned char c = pow(256,sizeof( c))-1;
      > > always work?[/color]
      >
      > Use the std::numeric_li mits class instead:
      >
      > #include <limits>
      >
      > int main()
      > {
      > unsigned char c = std::numeric_li mits<unsigned char>::max();
      > unsigned int i = std::numeric_li mits<unsigned int>::max();
      > return 0;
      > }[/color]

      Thanks!

      Regards,
      Matthias


      Comment

      • Claudio Jolowicz

        #4
        Re: Initialize int and char with maximum values

        On Wed, 21 Apr 2004, Der Andere wrote:
        [color=blue]
        >I want to initialize unsigned int and unsigned char with their[/color]
        maximum[color=blue]
        >value.
        >Just for being sure: Does
        > unsigned int i = pow(256,sizeof( i))-1;
        > unsigned char c = pow(256,sizeof( c))-1;
        >always work?[/color]



        The unit of sizeof is the size of a char. A char can have more than 8
        bits in an implementation. So no, your solution is not guaranteed to
        work. Instead, use:

        #include <limits>

        int main()
        {
        unsigned int i = std::numeric_li mits<unsigned int>::max();
        unsigned char c = std::numeric_li mits<unsigned char>::max();

        return 0;
        }


        An ugly and unsafe (?) alternative:

        int main()
        {
        unsigned int i = static_cast<uns igned int>(-1);
        unsigned char c = static_cast<uns igned char>(-1);
        }

        The reason this might not work is that it assumes twos complement
        representation of signed integers.

        Cheers,
        Claudio.

        --
        Claudio Jolowicz




        Comment

        • Pete Becker

          #5
          Re: Initialize int and char with maximum values

          Der Andere wrote:[color=blue]
          >
          > I want to initialize unsigned int and unsigned char with their maximum
          > value.
          > Just for being sure: Does
          > unsigned int i = pow(256,sizeof( i))-1;
          > unsigned char c = pow(256,sizeof( c))-1;
          > always work?
          >[/color]

          Since your types are unsigned, all you need is this:

          unsigned int i = -1;
          unsigned char c = -1;

          --

          Pete Becker
          Dinkumware, Ltd. (http://www.dinkumware.com)

          Comment

          • Brian Rodenborn

            #6
            Re: Initialize int and char with maximum values


            Pete Becker <petebecker@acm .org> wrote in message
            news:40870619.9 DCECCEE@acm.org ...
            [color=blue]
            > Since your types are unsigned, all you need is this:
            >
            > unsigned int i = -1;
            > unsigned char c = -1;[/color]


            There's always good-fashioned UCHAR_MAX and UINT_MAX.



            Brian Rodenborn


            Comment

            • Siemel Naran

              #7
              Re: Initialize int and char with maximum values

              "Pete Becker" <petebecker@acm .org> wrote in message
              [color=blue]
              > Since your types are unsigned, all you need is this:
              >
              > unsigned int i = -1;
              > unsigned char c = -1;[/color]

              Sounds right, and I've used it before, except casting on the right hand side
              to avoid compiler warnings.

              unsigned int i = (unsigned int)(-1);

              But did you see Claudio's reply, where he talks of twos complement?

              Claudio wronte:[color=blue]
              > unsigned int i = static_cast<uns igned int>(-1);
              > unsigned char c = static_cast<uns igned char>(-1);
              > The reason this might not work is that it assumes twos complement
              > representation of signed integers.[/color]


              Comment

              • Jerry Coffin

                #8
                Re: Initialize int and char with maximum values

                Claudio Jolowicz <cj603@doc.ic.a c.uk> wrote in message news:<Pine.LNX. 4.58.0404211408 280.15368@siski n.doc.ic.ac.uk> ...

                [ ... ]
                [color=blue]
                > An ugly and unsafe (?) alternative:
                >
                > int main()
                > {
                > unsigned int i = static_cast<uns igned int>(-1);
                > unsigned char c = static_cast<uns igned char>(-1);
                > }
                >
                > The reason this might not work is that it assumes twos complement
                > representation of signed integers.[/color]

                At least as I read it, section 3.9.1/4 of the standard requires this to work.
                Later,
                Jerry.

                --
                The universe is a figment of its own imagination.

                Comment

                • Jack Klein

                  #9
                  Re: Initialize int and char with maximum values

                  On Wed, 21 Apr 2004 14:19:00 +0100, Claudio Jolowicz
                  <cj603@doc.ic.a c.uk> wrote in comp.lang.c++:
                  [color=blue]
                  > On Wed, 21 Apr 2004, Der Andere wrote:
                  >[color=green]
                  > >I want to initialize unsigned int and unsigned char with their[/color]
                  > maximum[color=green]
                  > >value.
                  > >Just for being sure: Does
                  > > unsigned int i = pow(256,sizeof( i))-1;
                  > > unsigned char c = pow(256,sizeof( c))-1;
                  > >always work?[/color]
                  >
                  > http://www.parashift.com/c++-faq-lit...sic-types.html
                  >
                  > The unit of sizeof is the size of a char. A char can have more than 8
                  > bits in an implementation. So no, your solution is not guaranteed to
                  > work. Instead, use:
                  >
                  > #include <limits>
                  >
                  > int main()
                  > {
                  > unsigned int i = std::numeric_li mits<unsigned int>::max();
                  > unsigned char c = std::numeric_li mits<unsigned char>::max();
                  >
                  > return 0;
                  > }
                  >
                  >
                  > An ugly and unsafe (?) alternative:
                  >
                  > int main()
                  > {
                  > unsigned int i = static_cast<uns igned int>(-1);
                  > unsigned char c = static_cast<uns igned char>(-1);
                  > }
                  >
                  > The reason this might not work is that it assumes twos complement
                  > representation of signed integers.
                  >
                  > Cheers,
                  > Claudio.[/color]

                  Not only is it required to work as you wrote it, it is required to
                  work without the cast:

                  unsigned int i = -1; // guaranteed to be UINT_MAX
                  unsigned char c = -1; // guaranteed to be UCHAR_MAX

                  --
                  Jack Klein
                  Home: http://JK-Technology.Com
                  FAQs for
                  comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
                  comp.lang.c++ http://www.parashift.com/c++-faq-lite/
                  alt.comp.lang.l earn.c-c++

                  Comment

                  • Jack Klein

                    #10
                    Re: Initialize int and char with maximum values

                    On Thu, 22 Apr 2004 03:26:35 GMT, "Siemel Naran"
                    <SiemelNaran@RE MOVE.att.net> wrote in comp.lang.c++:
                    [color=blue]
                    > "Pete Becker" <petebecker@acm .org> wrote in message
                    >[color=green]
                    > > Since your types are unsigned, all you need is this:
                    > >
                    > > unsigned int i = -1;
                    > > unsigned char c = -1;[/color]
                    >
                    > Sounds right, and I've used it before, except casting on the right hand side
                    > to avoid compiler warnings.
                    >
                    > unsigned int i = (unsigned int)(-1);
                    >
                    > But did you see Claudio's reply, where he talks of twos complement?[/color]

                    Yes I did, and I replied to it. Claudio is completely incorrect.
                    Initialization and assignment to arithmetic types in C++ is exactly
                    the same as it is and always has been in C, and that is defined in
                    terms of value, not representation.

                    Assigning or initializing any of the unsigned integer types with -1 is
                    guaranteed to set that unsigned type to its maximum value, regardless
                    of the bit-wise representation of -1 on that architecture.

                    What is NOT guaranteed to work is something like this:

                    int si = -1;
                    int u1 = *(unsigned int *)&si;

                    Here you would be bypassing the actual value of -1 and copying the bit
                    pattern directly.

                    --
                    Jack Klein
                    Home: http://JK-Technology.Com
                    FAQs for
                    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
                    comp.lang.c++ http://www.parashift.com/c++-faq-lite/
                    alt.comp.lang.l earn.c-c++

                    Comment

                    • Claudio Puviani

                      #11
                      Re: Initialize int and char with maximum values

                      "Jack Klein" <jackklein@spam cop.net> wrote[color=blue][color=green]
                      > > But did you see Claudio [Jolowicz]'s reply, where he talks
                      > > of twos complement?[/color]
                      >
                      > Yes I did, and I replied to it. Claudio [Jolowicz] is
                      > completely incorrect. Initialization and assignment to
                      > arithmetic types in C++ is exactly the same as it is
                      > and always has been in C, and that is defined in
                      > terms of value, not representation.[/color]

                      I wish you people would use last names. ;-)

                      Claudio Puviani


                      Comment

                      • Pete Becker

                        #12
                        Re: Initialize int and char with maximum values

                        Siemel Naran wrote:[color=blue]
                        >
                        > "Pete Becker" <petebecker@acm .org> wrote in message
                        >[color=green]
                        > > Since your types are unsigned, all you need is this:
                        > >
                        > > unsigned int i = -1;
                        > > unsigned char c = -1;[/color]
                        >
                        > Sounds right, and I've used it before, except casting on the right hand side
                        > to avoid compiler warnings.
                        >[/color]

                        Complain to the compiler writer, or turn of the $"^#&$ warnings.
                        Cluttering code with unnecessary casts makes it harder to read and
                        harder to maintain. Write code according to your standards, not a
                        standard imposed by some compiler writer who knows nothing about the
                        problems you're trying to solve. (end of rant)

                        --

                        Pete Becker
                        Dinkumware, Ltd. (http://www.dinkumware.com)

                        Comment

                        Working...