C++编程思想 第2卷 第7章 通用容器 持有二进制位 bitset<n>

bitset作为模板接受一个无符号整形模板参数
该参数用来表示二进制位的位数
因此bitset<10>与bitset<20>相比就是两种不同的类型
不能在它们两个之间进行比较 赋值 等操作

//: C07:BitSet.cpp {-bor}
// From "Thinking in C++, Volume 2", by Bruce Eckel & Chuck Allison.
// (c) 1995-2004 MindView, Inc. All Rights Reserved.
// See source code use permissions stated in the file 'License.txt',
// distributed with the code package available at www.MindView.net.
// Exercising the bitset class.
#include <bitset>
#include <climits>
#include <cstdlib>
#include <ctime>
#include <cstddef>
#include <iostream>
#include <string>
using namespace std;

const int SZ = 32;
typedef bitset<SZ> BS;

template<int bits> bitset<bits> randBitset() {
  bitset<bits> r(rand());
  for(int i = 0; i < bits/16 - 1; i++) {
    r <<= 16;
    // "OR" together with a new lower 16 bits:
    r |= bitset<bits>(rand());
  }
  return r;
}

int main() {
  srand(time(0));
  cout << "sizeof(bitset<16>) = "
       << sizeof(bitset<16>) << endl;
  cout << "sizeof(bitset<32>) = "
       << sizeof(bitset<32>) << endl;
  cout << "sizeof(bitset<48>) = "
       << sizeof(bitset<48>) << endl;
  cout << "sizeof(bitset<64>) = "
       << sizeof(bitset<64>) << endl;
  cout << "sizeof(bitset<65>) = "
       << sizeof(bitset<65>) << endl;
  BS a(randBitset<SZ>()), b(randBitset<SZ>());
  // Converting from a bitset:
  unsigned long ul = a.to_ulong();
  cout << a << endl;
  // Converting a string to a bitset:
  string cbits("111011010110111");
  cout << "as a string = " << cbits <<endl;
  cout << BS(cbits) << " [BS(cbits)]" << endl;
  cout << BS(cbits, 2) << " [BS(cbits, 2)]" << endl;
  cout << BS(cbits, 2, 11) << " [BS(cbits, 2, 11)]"<< endl;
  cout << a << " [a]" << endl;
  cout << b << " [b]" << endl;
  // Bitwise AND:
  cout << (a & b) << " [a & b]" << endl;
  cout << (BS(a) &= b) << " [a &= b]" << endl;
  // Bitwise OR:
  cout << (a | b) << " [a | b]" << endl;
  cout << (BS(a) |= b) << " [a |= b]" << endl;
  // Exclusive OR:
  cout << (a ^ b) << " [a ^ b]" << endl;
  cout << (BS(a) ^= b) << " [a ^= b]" << endl;
  cout << a << " [a]" << endl; // For reference
  // Logical left shift (fill with zeros):
  cout << (BS(a) <<= SZ/2) << " [a <<= (SZ/2)]" << endl;
  cout << (a << SZ/2) << endl;
  cout << a << " [a]" << endl; // For reference
  // Logical right shift (fill with zeros):
  cout << (BS(a) >>= SZ/2) << " [a >>= (SZ/2)]" << endl;
  cout << (a >> SZ/2) << endl;
  cout << a << " [a]" << endl; // For reference
  cout << BS(a).set() << " [a.set()]" << endl;
  for(int i = 0; i < SZ; i++)
    if(!a.test(i)) {
      cout << BS(a).set(i)
           << " [a.set(" << i <<")]" << endl;
      break; // Just do one example of this
    }
  cout << BS(a).reset() << " [a.reset()]"<< endl;
  for(int j = 0; j < SZ; j++)
    if(a.test(j)) {
      cout << BS(a).reset(j)
           << " [a.reset(" << j <<")]" << endl;
      break; // Just do one example of this
    }
  cout << BS(a).flip() << " [a.flip()]" << endl;
  cout << ~a << " [~a]" << endl;
  cout << a << " [a]" << endl; // For reference
  cout << BS(a).flip(1) << " [a.flip(1)]"<< endl;
  BS c;
  cout << c << " [c]" << endl;
  cout << "c.count() = " << c.count() << endl;
  cout << "c.any() = "
       << (c.any() ? "true" : "false") << endl;
  cout << "c.none() = "
       << (c.none() ? "true" : "false") << endl;
  c[1].flip(); c[2].flip();
  cout << c << " [c]" << endl;
  cout << "c.count() = " << c.count() << endl;
  cout << "c.any() = "
       << (c.any() ? "true" : "false") << endl;
  cout << "c.none() = "
       << (c.none() ? "true" : "false") << endl;
  // Array indexing operations:
  c.reset();
  for(size_t k = 0; k < c.size(); k++)
    if(k % 2 == 0)
      c[k].flip();
  cout << c << " [c]" << endl;
  c.reset();
  // Assignment to bool:
  for(size_t ii = 0; ii < c.size(); ii++)
    c[ii] = (rand() % 100) < 25;
  cout << c << " [c]" << endl;
  // bool test:
  if(c[1])
    cout << "c[1] == true";
  else
    cout << "c[1] == false" << endl;
  getchar();
} ///:~

输出
sizeof(bitset<16>) = 4
sizeof(bitset<32>) = 4
sizeof(bitset<48>) = 8
sizeof(bitset<64>) = 8
sizeof(bitset<65>) = 16
01000010010101110010010110000010
as a string = 111011010110111
00000000000000000111011010110111 [BS(cbits)]
00000000000000000001011010110111 [BS(cbits, 2)]
00000000000000000000010110101101 [BS(cbits, 2, 11)]
01000010010101110010010110000010 [a]
00101101000010100101110100000010 [b]
00000000000000100000010100000010 [a & b]
00000000000000100000010100000010 [a &= b]
01101111010111110111110110000010 [a | b]
01101111010111110111110110000010 [a |= b]
01101111010111010111100010000000 [a ^ b]
01101111010111010111100010000000 [a ^= b]
01000010010101110010010110000010 [a]
00100101100000100000000000000000 [a <<= (SZ/2)]
00100101100000100000000000000000
01000010010101110010010110000010 [a]
00000000000000000100001001010111 [a >>= (SZ/2)]
00000000000000000100001001010111
01000010010101110010010110000010 [a]
11111111111111111111111111111111 [a.set()]
01000010010101110010010110000011 [a.set(0)]
00000000000000000000000000000000 [a.reset()]
01000010010101110010010110000000 [a.reset(1)]
10111101101010001101101001111101 [a.flip()]
10111101101010001101101001111101 [~a]
01000010010101110010010110000010 [a]
01000010010101110010010110000000 [a.flip(1)]
00000000000000000000000000000000 [c]
c.count() = 0
c.any() = false
c.none() = true
00000000000000000000000000000110 [c]
c.count() = 2
c.any() = true
c.none() = false
01010101010101010101010101010101 [c]
10000000010000010000010010000000 [c]
c[1] == false

为产生有趣的随机bitset
在程序中创建了函数randBitset()
该函数将每16个随机二进制向左移动
知道bitset被填满为止

猜你喜欢

转载自blog.csdn.net/eyetired/article/details/82530999