Detailed STL (14) - introduction and use of bitset (bitmap)

Introduction to bitset

The introduction of bitmaps

Gives 4 billion unique unsigned integers, unsorted. Given an unsigned integer, how to quickly determine whether a number is among the 4 billion numbers?

To determine whether a number is in a certain heap, we may think of the following methods:

  • Sort this pile of numbers, and then use the binary search method to determine whether the number is in this pile of numbers.
  • Insert this pile of numbers into the unordered_set container, and then call the find function to determine whether the number is in this pile of numbers.

From the method point of view, both methods are possible, and the efficiency is also good. The time complexity of the first method is O ( N log N ) O(NlogN)O ( N l o g N ) , the time complexity of the second method isO ( N ) O(N)O ( N ) _

But the problem is that there are 4 billion numbers here. If we want to load all these numbers into memory, it will take up 16G of space, and the space consumption is very large. Therefore, from the perspective of space consumption, the above two methods are actually infeasible.

Bitmap resolution

In fact, in this problem, we only need to judge whether a number is present or not, that is, there are only two states, then we can use a bit to indicate whether the data exists, if the bit is 1, it means it exists, and the bit is 0. Indicates that it does not exist. For example, there are 232
insert image description here
unsigned integers in total , so recording these numbers requires 232 bits, which is 512M of memory space, and memory consumption is greatly reduced.

bitmap concept

The so-called bitmap is to use each bit to store a certain state, which is suitable for scenarios with massive data and no data repetition. It is usually used to determine whether a certain data exists or not.

Bitmap applications

Common bitmap applications are as follows:

  1. Quickly find if a piece of data is in a collection.
  2. sort.
  3. Find the intersection, union, etc. of two sets.
  4. Disk block mark in the operating system.
  5. Signal flag bits in the kernel (signal mask word and pending signal set).

Use of bitset

How a bitset is defined

Method 1: Construct a 16-bit bitmap, all bits are initialized to 0.

bitset<16> bs1; //0000000000000000

Method 2: Construct a 16-bit bitmap, and initialize the first n bits of the bitmap according to the given value.

bitset<16> bs2(0xfa5); //0000111110100101

Method 3: Construct a 16-bit bitmap, and initialize the first n bits of the bitmap according to the 0/1 sequence in the string.

bitset<16> bs3(string("10111001")); //0000000010111001

Use of bitset member functions

Commonly used member functions in bitset are as follows:

member function Function
set Set specified bit or all bits
reset Clear specified bit or all bits
flip Invert specified bits or all bits
test Get the state of the specified bit
count Get the number of bits that are set
size Get the number of bits that can be accommodated
any Returns true if any of the bits are set
none Returns true if no bit is set
all Returns true if all bits are set

Example of use:

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

int main()
{
    
    
	bitset<8> bs;
	bs.set(2); //设置第2位
	bs.set(4); //设置第4位
	cout << bs << endl; //00010100
	
	bs.flip(); //反转所有位
	cout << bs << endl; //11101011
	cout << bs.count() << endl; //6

	cout << bs.test(3) << endl; //1

	bs.reset(0); //清空第0位
	cout << bs << endl; //11101010

	bs.flip(7); //反转第7位
	cout << bs << endl; //01101010

	cout << bs.size() << endl; //8

	cout << bs.any() << endl; //1

	bs.reset(); //清空所有位
	cout << bs.none() << endl; //1

	bs.set(); //设置所有位
	cout << bs.all() << endl; //1
	return 0;
}

Note: When using the member functions set, reset, and flip, if a certain bit is specified, the bit is operated, and if no bit is specified, all bits are operated.

Use of bitset operator

1. The use of >> and << operators in bitset.
The bitset container overloads the >> and << operators. We can directly use the >> and << operators to perform input and output operations on the objects defined by the biset container.

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

int main()
{
    
    
	bitset<8> bs;
	cin >> bs; //10110
	cout << bs << endl; //00010110
	return 0;
}

Second, the use of assignment operators, relational operators, compound assignment operators, and unary operators in bitset.
In the bitset container, not only the assignment operator and some relational operators are overloaded, but also some compound assignment operators and unary operators are overloaded, and we can directly use these operators to operate on individual bitmaps.

Include the following operators:

  • Assignment operator: =.
  • Relational operators: ==, !=.
  • Compound assignment operators: &=, |=, ^=, <<=, >>=.
  • Unary operator: ~.
#include <iostream>
#include <string>
#include <bitset>
using namespace std;

int main()
{
    
    
	bitset<8> bs1(string("10101010"));
	bitset<8> bs2(string("10101010"));
	bs1 >>= 1;
	cout << bs1 << endl; //01010101

	bs2 |= bs1;
	cout << bs2 << endl; //11111111
	return 0;
}

3. The use of bitwise operators in bitset.
The three bit operators are also overloaded in the bitset container. We can directly use &, |, ^ to operate on each bitmap.

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

int main()
{
    
    
	bitset<8> bs1(string("10101010"));
	bitset<8> bs2(string("01010101"));
	
	cout << (bs1&bs2) << endl; //00000000
	cout << (bs1|bs2) << endl; //11111111
	cout << (bs1^bs2) << endl; //11111111
	return 0;
}

Fourth, the use of the [ ] operator in bitset.
The [ ] operator is overloaded in the bitset container, and we can directly use [ ] to access or modify the specified bit.

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

int main()
{
    
    
	bitset<8> bs(string("00110101"));
	cout << bs[0] << endl; //1
	bs[0] = 0;
	cout << bs << endl; //00110100
	return 0;
}

Guess you like

Origin blog.csdn.net/chenlong_cxy/article/details/122508805
Recommended