Initialize int and char with maximum values

Discussion in 'C++' started by Der Andere, Apr 21, 2004.

  1. Der Andere

    Der Andere Guest

    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
    Der Andere, Apr 21, 2004
    #1
    1. Advertising

  2. Der Andere wrote:
    > 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?


    Use the std::numeric_limits class instead:

    #include <limits>

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

    --
    Peter van Merkerk
    peter.van.merkerk(at)dse.nl
    Peter van Merkerk, Apr 21, 2004
    #2
    1. Advertising

  3. Der Andere

    Der Andere Guest

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

    >
    > Use the std::numeric_limits class instead:
    >
    > #include <limits>
    >
    > int main()
    > {
    > unsigned char c = std::numeric_limits<unsigned char>::max();
    > unsigned int i = std::numeric_limits<unsigned int>::max();
    > return 0;
    > }


    Thanks!

    Regards,
    Matthias
    Der Andere, Apr 21, 2004
    #3
  4. On Wed, 21 Apr 2004, Der Andere wrote:

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


    http://www.parashift.com/c -faq-lite/intrinsic-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_limits<unsigned int>::max();
    unsigned char c = std::numeric_limits<unsigned char>::max();

    return 0;
    }


    An ugly and unsafe (?) alternative:

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

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

    Cheers,
    Claudio.

    --
    Claudio Jolowicz
    http://www.doc.ic.ac.uk/~cj603
    Claudio Jolowicz, Apr 21, 2004
    #4
  5. Der Andere

    Pete Becker Guest

    Der Andere wrote:
    >
    > 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?
    >


    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)
    Pete Becker, Apr 22, 2004
    #5
  6. Pete Becker <> wrote in message
    news:...

    > Since your types are unsigned, all you need is this:
    >
    > unsigned int i = -1;
    > unsigned char c = -1;



    There's always good-fashioned UCHAR_MAX and UINT_MAX.



    Brian Rodenborn
    Brian Rodenborn, Apr 22, 2004
    #6
  7. Der Andere

    Siemel Naran Guest

    "Pete Becker" <> wrote in message

    > Since your types are unsigned, all you need is this:
    >
    > unsigned int i = -1;
    > unsigned char c = -1;


    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:
    > unsigned int i = static_cast<unsigned int>(-1);
    > unsigned char c = static_cast<unsigned char>(-1);
    > The reason this might not work is that it assumes twos complement
    > representation of signed integers.
    Siemel Naran, Apr 22, 2004
    #7
  8. Der Andere

    Jerry Coffin Guest

    Claudio Jolowicz <> wrote in message news:<>...

    [ ... ]

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


    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.
    Jerry Coffin, Apr 22, 2004
    #8
  9. Der Andere

    Jack Klein Guest

    On Wed, 21 Apr 2004 14:19:00 +0100, Claudio Jolowicz
    <> wrote in comp.lang.c++:

    > On Wed, 21 Apr 2004, Der Andere wrote:
    >
    > >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?

    >
    > http://www.parashift.com/c -faq-lite/intrinsic-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_limits<unsigned int>::max();
    > unsigned char c = std::numeric_limits<unsigned char>::max();
    >
    > return 0;
    > }
    >
    >
    > An ugly and unsafe (?) alternative:
    >
    > int main()
    > {
    > unsigned int i = static_cast<unsigned int>(-1);
    > unsigned char c = static_cast<unsigned char>(-1);
    > }
    >
    > The reason this might not work is that it assumes twos complement
    > representation of signed integers.
    >
    > Cheers,
    > Claudio.


    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.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
    Jack Klein, Apr 22, 2004
    #9
  10. Der Andere

    Jack Klein Guest

    On Thu, 22 Apr 2004 03:26:35 GMT, "Siemel Naran"
    <> wrote in comp.lang.c++:

    > "Pete Becker" <> wrote in message
    >
    > > Since your types are unsigned, all you need is this:
    > >
    > > unsigned int i = -1;
    > > unsigned char c = -1;

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


    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.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
    Jack Klein, Apr 22, 2004
    #10
  11. "Jack Klein" <> wrote
    > > But did you see Claudio [Jolowicz]'s reply, where he talks
    > > of twos complement?

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


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

    Claudio Puviani
    Claudio Puviani, Apr 22, 2004
    #11
  12. Der Andere

    Pete Becker Guest

    Siemel Naran wrote:
    >
    > "Pete Becker" <> wrote in message
    >
    > > Since your types are unsigned, all you need is this:
    > >
    > > unsigned int i = -1;
    > > unsigned char c = -1;

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


    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)
    Pete Becker, Apr 22, 2004
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Schnoffos
    Replies:
    2
    Views:
    1,208
    Martien Verbruggen
    Jun 27, 2003
  2. trey

    newbie: char* int and char *int

    trey, Sep 10, 2003, in forum: C Programming
    Replies:
    7
    Views:
    403
    Irrwahn Grausewitz
    Sep 10, 2003
  3. Hal Styli
    Replies:
    14
    Views:
    1,629
    Old Wolf
    Jan 20, 2004
  4. lovecreatesbeauty
    Replies:
    1
    Views:
    1,043
    Ian Collins
    May 9, 2006
  5. gert
    Replies:
    20
    Views:
    1,155
Loading...

Share This Page