Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Initialize int and char with maximum values

Reply
Thread Tools

Initialize int and char with maximum values

 
 
Der Andere
Guest
Posts: n/a
 
      04-21-2004
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


 
Reply With Quote
 
 
 
 
Peter van Merkerk
Guest
Posts: n/a
 
      04-21-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?


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


 
Reply With Quote
 
 
 
 
Der Andere
Guest
Posts: n/a
 
      04-21-2004
> > 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


 
Reply With Quote
 
Claudio Jolowicz
Guest
Posts: n/a
 
      04-21-2004
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-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_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



 
Reply With Quote
 
Pete Becker
Guest
Posts: n/a
 
      04-21-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?
>


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)
 
Reply With Quote
 
Brian Rodenborn
Guest
Posts: n/a
 
      04-22-2004

Pete Becker <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...

> 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


 
Reply With Quote
 
Siemel Naran
Guest
Posts: n/a
 
      04-22-2004
"Pete Becker" <(E-Mail Removed)> 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.



 
Reply With Quote
 
Jerry Coffin
Guest
Posts: n/a
 
      04-22-2004
Claudio Jolowicz <(E-Mail Removed)> wrote in message news:<(E-Mail Removed) .ic.ac.uk>...

[ ... ]

> 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.
 
Reply With Quote
 
Jack Klein
Guest
Posts: n/a
 
      04-22-2004
On Wed, 21 Apr 2004 14:19:00 +0100, Claudio Jolowicz
<(E-Mail Removed)> 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-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_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/~a...FAQ-acllc.html
 
Reply With Quote
 
Jack Klein
Guest
Posts: n/a
 
      04-22-2004
On Thu, 22 Apr 2004 03:26:35 GMT, "Siemel Naran"
<(E-Mail Removed)> wrote in comp.lang.c++:

> "Pete Becker" <(E-Mail Removed)> 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/~a...FAQ-acllc.html
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
int urldecode(char *src, char *last, char *dest) gert C Programming 20 02-16-2007 11:28 PM
(const char *cp) and (char *p) are consistent type, (const char **cpp) and (char **pp) are not consistent lovecreatesbeauty C Programming 1 05-09-2006 08:01 AM
int main(int argc, char *argv[] ) vs int main(int argc, char **argv ) Hal Styli C Programming 14 01-20-2004 10:00 PM
newbie: char* int and char *int trey C Programming 7 09-10-2003 03:24 AM
dirty stuff: f(int,int) cast to f(struct{int,int}) Schnoffos C Programming 2 06-27-2003 03:13 AM



Advertisments