#include <bitset>
#include <iostream>
#include <string>
template <typename T>
std::string getBinary(T t) {
unsigned long val = t;
std::bitset<8 * sizeof t> bits(val);
return bits.to_string();
}
int main() {
using namespace std;
while (cin) {
unsigned short x;
cin >> x;
cout << getBinary(x) << '\n';
}
}
An occasionally updated blog, mostly related to programming. (Views are my own, not my employer's.)
Monday, December 9, 2013
Binary representation of an int type value with bitset::to_string
Get the binary representation of an integral type using bitset's to_string method:
Subscribe to:
Comments (Atom)