位图的实现和二进制打印方法

#include 
#include 
#include 

using namespace std;

int main() {
     
	unsigned char ack_buffer[15] = {
      0 };
	vector<int> exist_seq;
	//每三个出现一次
	for (int i = 0; i < 15*8; i++) {
     
		if (i % 3 == 0) {
     
			exist_seq.push_back(i);
		}
	}
	//原始数据
	for (auto item : exist_seq) {
     
		cout << item;
	}
	cout << endl;
	//构建位图
	for (int i = 0; i < exist_seq.size(); i++) {
     
		ack_buffer[exist_seq[i] >> 3] |= 1 << (exist_seq[i] % 8);
	}
	//二进制打印
	for (auto item:ack_buffer) {
     
		for (int i = 0; i < 8; i++) {
     
			if (item & (0x01<<i)) {
     
				cout << 1;
			}
			else {
     
				cout << 0;
			}
		}
	}
	cout << endl;
	//exit(0);
	system("pause");
	return 0;
}

位图的实现和二进制打印方法_第1张图片

你可能感兴趣的:(CPP)