- Convert decimal to binary string using std::bitset
int n = 10000;
string s = bitset<32>(n).to_string(); // 32 is size of n (int)
std::string binary = std::bitset<8>(n).to_string();
#include<iostream>
#include<vector>
using namespace std;
void Decimal_To_Binary(int num)
{
string s;
vector<int> v;
int i = 0;
while (num != 0)
{
v.push_back(num % 2);
num /= 2;
i++;
}
reverse(v.begin(), v.end());
for (auto n : v)
{
cout << n;
}
//or
/*for (int i = 0; i<v.size(); i++)
{
cout<< v[i];
}*/
}
int main()
{
int num;
cin >> num;
Decimal_To_Binary(num);
return 0;
}
long long x;
scanf("%lld", &x);
long long n = ceil(log2((float)x));
int bin[n];
for(int i = n - 1; i >= 0; --i)
{
if (x & (1LL << i))
{
bin[n - 1 -i] = 1;
}
else
{
bin[n - 1 - i] = 0;
}
}
for(int i = 0;i < n; ++i) printf("%d", bin[i]);