Initialize int and char with maximum values

D

Der Andere

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
 
P

Peter van Merkerk

Der said:
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;
}
 
D

Der Andere

I want to initialize unsigned int and unsigned char with their maximum
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
 
C

Claudio Jolowicz

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

Pete Becker

Der said:
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;
 
B

Brian Rodenborn

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
 
S

Siemel Naran

Pete Becker said:
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:
 
J

Jerry Coffin

[ ... ]
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.
 
J

Jack Klein

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
 
J

Jack Klein

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

Claudio Puviani

Jack Klein said:
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
 
P

Pete Becker

Siemel said:
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)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top