例子一、十进制转换二进制
// bitset_bitset.cpp
// compile with: /EHsc
#include <bitset>
#include <iostream>
int main( )
{
// Using the default constructor
using namespace std;
// Using the second member function
bitset<20> b1 ( 5 );
cout << "The set of bits in bitset<5> b1( 5 ) is: ( "
<< b1 << " )." << endl;
}
例子二、二进制转换十进制
#include <bitset>
#include <iostream>
void main()
{
using namespace std;
string bitval5 ("11110011011");
int length = strlen("11110011011");
bitset<11> b5 ( bitval5);
cout << b5 << endl;
cout << b5.to_ulong( ) << endl;
}
例子三、支持指针
#include <bitset>
#include <iostream>
void main()
{
using namespace std;
char p[] = "11110011011";
bitset<50> b5 ( p);
cout << b5 << endl;
cout << b5.to_ulong( ) << endl;
}