黑白程式

黑白程式

导航

位集合学习代码

 

 

#include<iostream>
#include<bitset>
using namespace std;
void display(bitset<8> bs)
{
 cout<<"Bitset=";
 for(int x=0;x<8;++x)
  cout<<bs[x];
 cout<<endl;
}

int main()
{
 bitset<8> MybitSet;
 MybitSet.set(1);
 MybitSet.set(2);
 MybitSet.set(3);
 MybitSet.set(5);
 MybitSet.set(7);
 display(MybitSet);
 for(int x=0;x<8;++x)
 {
  cout<<"Bit"<<x<<" is ";
  if(MybitSet.test (x))
   cout<<"set";
  else
   cout<<"unset";
  cout<<endl;
 }

 return 0;
}
/*运行结果如下:
Bitset=01110101
Bit0 is unset
Bit1 is set
Bit2 is set
Bit3 is set
Bit4 is unset
Bit5 is set
Bit6 is unset
Bit7 is set
Press any key to continue
*/

posted on 2008-09-16 10:17  黑白程式  阅读(110)  评论(0编辑  收藏  举报