📜  C++位集有趣的事实(1)

📅  最后修改于: 2023-12-03 14:39:59.344000             🧑  作者: Mango

C++位集有趣的事实

在C++中,位集是一种非常有用的数据结构,可以节省内存并提高程序效率。以下是C++位集的有趣事实:

1. 单个位操作

可以使用位运算符(&、|、^、~)来对位集进行单个位操作。这些操作包括与、或、异或和取反。例如:

#include <bitset>
#include <iostream>
using namespace std;

int main() {
  bitset<8> bits("00011100");
  cout << "Original bits: " << bits << endl;

  bits.set(1); // 设置第二位为1
  cout << "After setting second bit: " << bits << endl;

  bits.set(); // 设置所有位为1
  cout << "After setting all bits: " << bits << endl;

  bits.reset(2); // 重置第三位为0
  cout << "After resetting third bit: " << bits << endl;

  bits.flip(); // 逐位取反
  cout << "After flipping all bits: " << bits << endl;

  return 0;
}

以上代码结果为:

Original bits: 00011100
After setting second bit: 00011110
After setting all bits: 11111111
After resetting third bit: 11111011
After flipping all bits: 00000100
2. 位运算符的实际应用

可以使用位运算符将多个标志组合起来,以获得更高效的代码。例如,使用按位与(&)运算符可以检查位集中是否同时设置了多个标志:

#include <bitset>
#include <iostream>
using namespace std;

int main() {
  enum Permissions {
    Read = 1,
    Write = 2,
    Execute = 4
  };

  bitset<8> permissions;
  permissions.set(Read);
  permissions.set(Write);

  if ((permissions & Read) != 0) {
    cout << "Read permission is set." << endl;
  }
  if ((permissions & Write) != 0) {
    cout << "Write permission is set." << endl;
  }
  if ((permissions & Execute) != 0) {
    cout << "Execute permission is set." << endl;
  }

  return 0;
}

以上代码结果为:

Read permission is set.
Write permission is set.
3. 按位移位运算符

可以使用按位移位运算符(<<、>>)将位集左移或右移任意数量的位。例如:

#include <bitset>
#include <iostream>
using namespace std;

int main() {
  bitset<8> bits("00011100");
  cout << "Original bits: " << bits << endl;

  bits <<= 2; // 左移两位
  cout << "After shifting left by 2: " << bits << endl;

  bits >>= 4; // 右移四位
  cout << "After shifting right by 4: " << bits << endl;

  return 0;
}

以上代码结果为:

Original bits: 00011100
After shifting left by 2: 01110000
After shifting right by 4: 00000111
4. 位集与整数转换

可以使用to_ulong()、to_ullong()、to_string()等方法将位集转换为整数或字符串,并使用from_ulong()、from_ullong()、from_string()等方法将整数或字符串转换为位集。例如:

#include <bitset>
#include <iostream>
using namespace std;

int main() {
  bitset<8> bits("00011100");
  unsigned long ulongValue = bits.to_ulong();
  cout << "Bits as unsigned long: " << ulongValue << endl;

  bits = bitset<8>(ulongValue);
  cout << "Bits as bitset again: " << bits << endl;

  string strValue = bits.to_string();
  cout << "Bits as string: " << strValue << endl;

  bits = bitset<8>(strValue);
  cout << "Bits as bitset again: " << bits << endl;

  return 0;
}

以上代码结果为:

Bits as unsigned long: 28
Bits as bitset again: 00011100
Bits as string: 00011100
Bits as bitset again: 00011100
5. 位集的初始化

可以使用字符串、整数、迭代器等多种方式来初始化位集。例如:

#include <bitset>
#include <iostream>
using namespace std;

int main() {
  // 从字符串初始化
  bitset<8> bits1("11001100");
  cout << "Bits from string: " << bits1 << endl;

  // 从整数初始化
  bitset<8> bits2(204);
  cout << "Bits from integer: " << bits2 << endl;

  // 从迭代器初始化
  char str[] = "11001100";
  bitset<8> bits3(str, str + 8);
  cout << "Bits from iterator: " << bits3 << endl;

  // 全部初始化为1
  bitset<8> bits4(0xFF);
  cout << "Bits from all 1's: " << bits4 << endl;

  // 全部初始化为0
  bitset<8> bits5;
  cout << "Bits from all 0's: " << bits5 << endl;

  return 0;
}

以上代码结果为:

Bits from string: 11001100
Bits from integer: 11001100
Bits from iterator: 11001100
Bits from all 1's: 11111111
Bits from all 0's: 00000000
6. 位集的操作符重载

可以重载位集的多个操作符,包括赋值操作符(=)、按位运算操作符(&、|、^、~)、比较操作符(==、!=、<、<=、>、>=)等。这使得位集更加便捷易用。例如:

#include <bitset>
#include <iostream>
using namespace std;

int main() {
  bitset<8> bits1("00011100");
  bitset<8> bits2("00111000");
  bitset<8> bits3 = bits1 & bits2;
  cout << "bits3: " << bits3 << endl;

  bitset<8> bits4 = bits1 | bits2;
  cout << "bits4: " << bits4 << endl;

  bitset<8> bits5 = bits1 ^ bits2;
  cout << "bits5: " << bits5 << endl;

  bitset<8> bits6 = ~bits1;
  cout << "bits6: " << bits6 << endl;

  if (bits1 == bits2) {
    cout << "bits1 is equal to bits2" << endl;
  } else {
    cout << "bits1 is not equal to bits2" << endl;
  }

  return 0;
}

以上代码结果为:

bits3: 00011000
bits4: 00111100
bits5: 00100100
bits6: 11100011
bits1 is not equal to bits2
7. 小结

C++的位集是一种非常有用的数据结构,可以用于节省内存和提高程序效率。本文介绍了C++位集的七个有趣的事实,包括单个位操作、按位运算符的实际应用、按位移位运算符、位集与整数转换、位集的初始化和操作符重载。这些技巧可以帮助程序员更好地使用C++位集来实现各种应用场景。