728x90
반응형

개념

10진수인 정수를 2진수로 출력하는 방법은 bitset 클래스를 이용한다.

#include <bitset>

char bit;

bitset<비트 수> (bit);

< > 안에 원하는 비트의 갯수를 입력하고 문자를 입력해주면 해당 문자가 원하는 갯수만큼 2진수로 출력된다.

 

 

예시

#include <iostream>
#include <bitset>

using namespace std;

int main(){
	char num = 10;
	
	cout << bitset<4>(num) << "\n";
	cout << bitset<8>(num) << "\n";
	cout << bitset<16>(num) << "\n";
	
	return 0;
}
1010
00001010
0000000000001010

 

 

참고 사이트

아래에 참고 사이트에 더 많은 예시와 설명이 되어있다.

bitset 클래스 | Microsoft Docs

cplusplus.com  :: bitset

https://www.crocus.co.kr/549

http://blog.naver.com/PostView.nhn?blogId=herbbread&logNo=220817372685&parentCategoryNo=&categoryNo=7&viewDate=&isShowPopularPosts=false&from=postView

728x90
반응형
복사했습니다!