Desc :

#include <bitset>

C++에서는 char, int로 비트연산을 하는 것보다 bitset이라는 컨테이너를 사용하는 것이 수월함

 

bitset<8> bit1

크기가 8비트인 변수를 선언

 

bit1.reset()

비트세트를 0으로 초기화


Source Code :

#include <iostream>
#include <bitset>

using namespace std;

int main()
{
	bitset<8> bit1;
	bit1.reset();	// 0000 0000
	bit1 = 127;		// 0111 1111

	bitset<8> bit2;
	bit2.reset();
	bit2 = 0x20;	// 32

	bitset<8> bit3 = bit1 & bit2;
	bitset<8> bit4 = bit1 | bit2;
	bitset<8> bit5 = bit1 ^ bit2;
	bitset<8> bit6 = ~bit1;
	bitset<8> bit7 = bit2 << 1;
	bitset<8> bit8 = bit2 >> 1;

	cout << "bit1 & bit2 : " << bit3 << ", " << bit3.to_ulong() << endl;
	cout << "bit1 | bit2 : " << bit4 << ", " << bit4.to_ulong() << endl;
	cout << "bit1 ^ bit2 : " << bit5 << ", " << bit5.to_ulong() << endl;
	cout << "~bit1 : " << bit6 << ", " << bit6.to_ulong() << endl;
	cout << "bit2 << 1  : " << bit7 << ", " << bit7.to_ulong() << endl;
	cout << "bit2 >> 1 : " << bit8 << ", " << bit8.to_ulong() << endl;

	return 0;
}

 


Result :

'C++' 카테고리의 다른 글

[C++] 030 명시적 변환  (0) 2021.03.18
[C++] 029 캐스트 연산자  (0) 2021.03.18
[C++] 027 쉼표 연산자  (0) 2021.03.18
[C++] 026 조건부 삼항 연산자  (0) 2021.03.18
[C++] 020 논리형 변수  (0) 2021.01.24

+ Recent posts