Search
 
SCRIPT & CODE EXAMPLE
 

CPP

inbuilt function to convert decimal to binary in c++

std::string binary = std::bitset<8>(n).to_string();
Comment

convert decimal to binary in c++

#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;
}
Comment

converting decimal to binary in cpp

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]);
Comment

PREVIOUS NEXT
Code Example
Cpp :: how to send email in c++ program 
Cpp :: iterate vector in reverse c++ 
Cpp :: check if char in string c++ 
Cpp :: remove decimal c++ 
Cpp :: c++ standard library source 
Cpp :: how to get the type of a variable in c++ 
Cpp :: string.begin() c++ 
Cpp :: how to split a string in c++ 
Cpp :: how to sort a string alphabetically in c++ 
Cpp :: how to find the sum of a vector c++ 
Cpp :: ue4 float to fstring 
Cpp :: for c++ 
Cpp :: how to sort in descending order in c++ 
Cpp :: c++ lambda 
Cpp :: initialize string with length c++ 
Cpp :: cpp create lambda with recursion 
Cpp :: conditional operator in c++ 
Cpp :: Header for INT_MIN 
Cpp :: how to know datatype of something in c++ 
Cpp :: sort array c++ 
Cpp :: c++ do every 1 minutes 
Cpp :: how many months have 31 days 
Cpp :: How to get cursor position c++ 
Cpp :: list in c++ 
Cpp :: how to remove first element from vector c++ 
Cpp :: c++ write to csv file append 
Cpp :: how to format decimal palces in c++ 
Cpp :: cpp get exception type 
Cpp :: stl in c++ 
Cpp :: how creat matrix column in c++ 
ADD CONTENT
Topic
Content
Source link
Name
7+4 =