C++ BIT集及其应用

位集是布尔值的数组,但每个布尔值不是单独存储的,而是位集优化了空间,使每个布尔值只占用1位空间,因此 位集B占用的空间小于布尔B[N]和向量B(N)占用的空间 .然而,位集的一个限制是, N必须在编译时已知,即常数 (矢量和动态数组不存在此限制)

null

由于位集以压缩的方式存储相同的信息,因此对位集的运算速度比数组和向量快。我们可以借助数组索引运算符[]单独访问位集的每一位,即bs[3]在位集bs的索引3处显示位,就像一个简单的数组一样。请记住,位集从向后开始索引,即10110,0位于第0和第3个索引,而1位于第1、2和第4个索引。 我们可以通过构造函数使用整数和二进制字符串构造一个位集,如下代码所示。位集的大小在编译时是固定的,也就是说,它不能在运行时更改。 这个 为位集类定义的主函数 操作员[]、计数、大小、设置、重置以及更多信息在下面的代码中进行了解释-

// C++ program to demonstrate various functionality of bitset
#include <bits/stdc++.h>
using namespace std;
#define M 32
int main()
{
// default constructor initializes with all bits 0
bitset<M> bset1;
// bset2 is initialized with bits of 20
bitset<M> bset2(20);
// bset3 is initialized with bits of specified binary string
bitset<M> bset3(string( "1100" ));
// cout prints exact bits representation of bitset
cout << bset1 << endl; // 00000000000000000000000000000000
cout << bset2 << endl; // 00000000000000000000000000010100
cout << bset3 << endl; // 00000000000000000000000000001100
cout << endl;
// declaring set8 with capacity of 8 bits
bitset<8> set8; // 00000000
// setting first bit (or 6th index)
set8[1] = 1; // 00000010
set8[4] = set8[1]; // 00010010
cout << set8 << endl;
// count function returns number of set bits in bitset
int numberof1 = set8.count();
// size function returns total number of bits in bitset
// so there difference will give us number of unset(0)
// bits in bitset
int numberof0 = set8.size() - numberof1;
cout << set8 << " has " << numberof1 << " ones and "
<< numberof0 << " zeros" ;
// test function return 1 if bit is set else returns 0
cout << "bool representation of " << set8 << " : " ;
for ( int i = 0; i < set8.size(); i++)
cout << set8.test(i) << " " ;
cout << endl;
// any function returns true, if atleast 1 bit
// is set
if (!set8.any())
cout << "set8 has no bit set." ;
if (!bset1.any())
cout << "bset1 has no bit set." ;
// none function returns true, if none of the bit
// is set
if (!bset1.none())
cout << "bset1 has some bit set" ;
// bset.set() sets all bits
cout << set8.set() << endl;
// bset.set(pos, b) makes bset[pos] = b
cout << set8.set(4, 0) << endl;
// bset.set(pos) makes bset[pos] = 1  i.e. default
// is 1
cout << set8.set(4) << endl;
// reset function makes all bits 0
cout << set8.reset(2) << endl;
cout << set8.reset() << endl;
// flip function flips all bits i.e.  1 <-> 0
// and  0 <-> 1
cout << set8.flip(2) << endl;
cout << set8.flip() << endl;
// Converting decimal number to binary by using bitset
int num = 100;
cout << "Decimal number: " << num
<< "  Binary equivalent: " << bitset<8>(num);
return 0;
}


输出:

00000000000000000000000000000000
00000000000000000000000000010100
00000000000000000000000000001100

00010010
00010010 has 2 ones and 6 zeros
bool representation of 00010010 : 0 1 0 0 1 0 0 0 
bset1 has no bit set.
11111111
11101111
11111111
11111011
00000000
00000100
11111011

Decimal number: 100 Binary equivalent: 01100100

对于位集,定义了重置和翻转功能。Set函数设置(1)如果未提供参数,则设置位集的所有位,否则设置其位置作为参数给出的位。同样,如果在没有参数的情况下调用reset和flip,它们也可以对整个位集执行操作,如果某个位置作为参数提供,则它们只在该位置执行操作。 对于位集,所有位运算符都是重载的,也就是说,它们可以直接应用于位集,而无需任何强制转换或转换,主要重载运算符是&、|、==、!=和移位运算符<>使对位集的操作变得容易。 下面的代码显示了上述运算符的用法。

// C++ program to show applicable operator on bitset.
#include <bits/stdc++.h>
using namespace std;
int main()
{
bitset<4> bset1(9); // bset1 contains 1001
bitset<4> bset2(3); // bset2 contains 0011
// comparison operator
cout << (bset1 == bset2) << endl; // false 0
cout << (bset1 != bset2) << endl; // true  1
// bitwise operation and assignment
cout << (bset1 ^= bset2) << endl; // 1010
cout << (bset1 &= bset2) << endl; // 0010
cout << (bset1 |= bset2) << endl; // 0011
// left and right shifting
cout << (bset1 <<= 2) << endl; // 1100
cout << (bset1 >>= 1) << endl; // 0110
// not operator
cout << (~bset2) << endl; // 1100
// bitwise operator
cout << (bset1 & bset2) << endl; // 0010
cout << (bset1 | bset2) << endl; // 0111
cout << (bset1 ^ bset2) << endl; // 0101
}


输出:

0
1
1010
0010
0011
1100
0110
1100
0010
0111
0101

本文由Utkarsh Trivedi撰稿。如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞14 分享