#include <bitset>
#include <iostream>
#include <string>
#include <limits>

using namespace std;
int main()
{
 cout<<"267 as binary short: "
  <<bitset<numeric_limits<unsigned short>::digits>(267)
  <<endl;

 cout<<"267 as binary long:  "
  <<bitset<numeric_limits<unsigned long>::digits>(267)
  <<endl;

 cout<<"10,000,000 with 24 bits: "
  <<bitset<24>(1e7)<<endl;

 cout<<"\"1000101011\" as number: "
  <<bitset<100>(string("1000101011")).to_ulong()<<endl;
}

记:

在c++的<limits>中定义了如下的一个模板类:

template<class Type> class numeric_limits


它可以求出下列内置类型的一些特性:
wchar_t, bool, char, signed char, unsigned char, short, unsigned short,
int, unsigned int, long, unsigned long, float, double, and long double.

有这么四个函数,比较有用:
max() Returns the maximum finite value for a type.


min() Returns the minimum normalized value for a type.

digits()   Returns the number of radix digits that the type can represent without loss of precision.

digits10()   Returns the number of decimal digits that the type can represent without loss of precision.

posted on 2010-04-13 19:25  蓝牙  阅读(139)  评论(0编辑  收藏  举报