(C++) 3.9 Bit flags, Bit masks 사용법

이준우·2021년 10월 12일
0
post-custom-banner

이건 무엇일까..? 코드를 보자

bit flags 사용법

#include <iostream>
#include <bitset>

int main()
{
	using namespace std;

	const unsigned char opt0 = 1 << 0;
	const unsigned char opt1 = 1 << 1;
	const unsigned char opt2 = 1 << 2;
	const unsigned char opt3 = 1 << 3;

	cout << bitset<8>(opt0) << endl;
	cout << bitset<8>(opt1) << endl;
	cout << bitset<8>(opt2) << endl;
	cout << bitset<8>(opt3) << endl;

	unsigned char items_flag = 0;

	cout << "No item" << bitset<8>(items_flag) << endl;

	// item0 on
	items_flag |= opt0;
	cout << "Item0 obtained " << bitset<8>(items_flag) << endl;
	
	// item3 on
	items_flag |= opt3;
	cout << "Item3 obtained " << bitset<8>(items_flag) << endl;

	// item3 lost
	items_flag &= ~opt3;
	cout << "Item lost " << bitset<8>(items_flag) << endl;

	// has item1?
	if (items_flag & opt1)
		cout << "Has item1" << endl;
	else
		cout << "Not have item1" << endl;
	

	// has item1?
	if (items_flag & opt0)
		cout << "Has item0" << endl;

	cout << bitset<8>(items_flag) << endl;
	
	//obtain item 2, 3
	items_flag |= (opt2 | opt3);
	cout << "Item0 obtained " << bitset<8>(items_flag) << endl;

	if ((items_flag & opt2) && !(items_flag & opt1))
	{
		items_flag ^= opt2;
		items_flag ^= opt1;
	}

	cout << bitset<8>(items_flag) << endl;
	
	return 0;
}

output : 00000001
	 00000010
	 00000100
	 00001000
	 No item00000000
	 Item0 obtained 00000001
	 Item3 obtained 00001001
	 Item lost 00000001
	 Not have item1
 	 Has item0
 	 00000001
	 Item0 obtained 00001101
 	 00001011

갑자기 뭔 코드가 이렇게 길까? 생각할 수 있으나 코드를 복붙하고 왜 이러한 과정이 나오는지 확인해 보도록 하자. 꼭 한번씩 입력하여 확인해보도록 한다.

bit masks 사용법

#include <iostream>
#include <bitset>
int main()
{
	using namespace std;

	const unsigned int red_mask = 0xFF0000;
	const unsigned int green_mask = 0x00FF00;
	const unsigned int blue_mask = 0x0000FF;

	cout << std::bitset<32>(red_mask) << endl;
	cout << std::bitset<32>(green_mask) << endl;
	cout << std::bitset<32>(blue_mask) << endl;

	unsigned int pixel_color = 0xDAA520;  
	// 20만 추출해보자.
	cout << std::bitset<32>(pixel_color) << endl;

	unsigned char blue = pixel_color & blue_mask;
	unsigned char green = (pixel_color & green_mask) >> 8 ;
	unsigned char red = (pixel_color & red_mask) >> 16;

	cout << "blue" << bitset<8>(blue) << " "  << int(blue) << endl;
	cout << "green" << bitset<8>(green) << " " << int(green) << endl;
	cout << "red" << bitset<8>(red) << " " << int(red) << endl;

	return 0;
}

output : 00000000111111110000000000000000
	 00000000000000001111111100000000
	 00000000000000000000000011111111
	 00000000110110101010010100100000
	 blue00100000 32
	 green10100101 165
	 red11011010 218

예제를 풀어보자!

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

int main(void)
{
	unsigned char option_viewed = 0x01;
	unsigned char option_edited = 0x02;
	unsigned char option_liked = 0x04;
	unsigned char option_shared = 0x08;
	unsigned char option_deleted = 0x80;

	unsigned char my_article_flags = 0;

	// view article
	my_article_flags |= option_viewed;
	cout << std::bitset<8>(my_article_flags) << endl;

	// like article
	my_article_flags |= option_liked;
	cout << std::bitset<8>(my_article_flags) << endl;

	// like article again
	my_article_flags ^= option_liked;
	cout << std::bitset<8>(my_article_flags) << endl;

	// delete article
	my_article_flags |= option_deleted;
	cout << std::bitset<8>(my_article_flags) << endl;

	return 0;
}
profile
꿈꾸는 CV
post-custom-banner

0개의 댓글