문제:
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 |
---|