'bitset'에 해당되는 글 1건

  1. 2015.10.24 C++ - 숫자를 binary (이진법) 형식으로 출력하기
C++2015. 10. 24. 20:57

문제:

C++를 이용하여 숫자를 binary (이진법)으로 출력하라.

 

 

풀이:

다음과 같이 <bitset>을 이용하면 간단하다.

 

main.cpp

#include <bitset>

#include <iostream>

 

using namespace std;

 

int main() {

  unsigned int a = 1234;

  cout << bitset<32>(a) << endl;

  return 0;

}

 

 

결과:

 

 

 

참고:

16진법 (hexadecimal), 8진법 (octal)은 다음과 같이 하면 된다.

 

main.cpp:

#include <iostream>

 

using namespace std;

 

int main() {

  int a = 1234;

  cout << dec << a << endl;

  cout << hex << a << endl;

  cout << oct << a << endl;

  return 0;

}

 

 

결과:

 

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

C++ - File (stream)에서 16진수 값 읽기  (0) 2015.11.17
Posted by topazus