使用最大值初始化int和char [英] Initialize int and char with maximum values
问题描述
我想用最大值
值初始化unsigned int和unsigned char。
只是为了确定:
unsigned int i = pow(256,sizeof(i)) - 1;
unsigned char c = pow(256,sizeof(c)) - 1;
一直有效吗?
谢谢,
Matthias
-
$ b $bFür电子邮件Anweisung in der Adresse befolgen
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写道:
Der Andere wrote:
我想初始化unsigned int和unsigned char及其最大值
。
只是为了确定:
unsigned int i = pow(256,sizeof(i)) - 1;
unsigned char c = pow(256,sizeof(c)) - 1 ;
总是有效吗?
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?
请改用std :: numeric_limits类:
#include< limits> ;
int main()
{
unsigned char c = std :: numeric_limits< unsigned char> :: max( );
unsigned int i = std :: numeric_limits< unsigned int> :: max();
返回0;
}
-
Peter van Merkerk
peter.van.merkerk(at)dse.nl
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
> >我想用最大的
> > I want to initialize unsigned int and unsigned char with their maximum
值来初始化unsigned int和unsigned char。
只是为了确定:
unsigned int i = pow( 256,sizeof(i)) - 1;
unsigned char c = pow(256,sizeof(c)) - 1;
始终有效吗?
value.
Just for being sure: Does
unsigned int i = pow(256,sizeof(i))-1;
unsigned char c = pow(256,sizeof(c))-1;
always work?
使用而是std :: numeric_limits类:
#include< limits>
int main()
{
unsigned char c = std :: numeric_limits< unsigned char> :: max();
unsigned int i = std :: numeric_limits< unsigned int> :: max();
return 0;
}
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;
}
谢谢!
问候,
Matthias
Thanks!
Regards,
Matthias
2004年4月21日星期三,Der Andere写道:
On Wed, 21 Apr 2004, Der Andere wrote:
我想用
最大值初始化unsigned int和unsigned char。
只是为了确定:
unsigned int i = pow(256,sizeof(i)) - 1;
unsigned char c = pow(256,sizeof(c)) - 1; <总是有效吗?
I want to initialize unsigned int and unsigned char with their maximumvalue.
Just for being sure: Does
unsigned int i = pow(256,sizeof(i))-1;
unsigned char c = pow(256,sizeof(c))-1;
always work?
sizeof的单位是一个炭的大小。一个char在一个实现中可以有超过8个
位。所以不,你的解决方案不能保证
工作。相反,使用:
#include< limits>
int main()
{
unsigned int i = std :: numeric_limits< unsigned int> :: max();
unsigned char c = std :: numeric_limits< unsigned char> :: max();
返回0;
}
丑陋且不安全(?)替代方案:
int main()
{
unsigned int i = static_cast< unsigned int>( - 1);
unsigned char c = static_cast< ; unsigned char>( - 1);
}
这可能不起作用的原因是它假定二元补充
签名整数的表示。
干杯,
克劳迪奥。
-
Claudio Jolowicz
http://www.doc.ic。 ac.uk/~cj603
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
这篇关于使用最大值初始化int和char的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!