C++ 이진수


개요

C++ 이진수

int 이진수 입력

#include <iostream>
using namespace std;

int main() {
    int a = 0b111;
    int b = 0b1000;
    cout << a << endl; // 7
    cout << b << endl; // 8
}

int 이진수 출력

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

int main() {
    int a = 7;
    int b = 8;
    cout << bitset<4>(a) << endl; // 0111
    cout << bitset<4>(b) << endl; // 1000
}

정수를 이진수문자열로 변환

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

int main() {
    int a = 7;
    int b = 8;
    string c = bitset<4>(a).to_string();
    string d = bitset<4>(b).to_string();
    cout << c << endl; // 0111
    cout << d << endl; // 1000
}

같이 보기