Search
 
SCRIPT & CODE EXAMPLE
 

CPP

convert decimal to binary c++

- Convert decimal to binary string using std::bitset
int n = 10000;
string s = bitset<32>(n).to_string(); // 32 is size of n (int)
Comment

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 :: c++ namespace example 
Cpp :: bfs sudocode 
Cpp :: google test assert stdout 
Cpp :: how to access a vector member by its index 
Cpp :: how to remove the scroll bar in pyqt6 
Cpp :: heredar constructor c++ 
Cpp :: Pseudocode of Dijkstra’s Algorithm in C++ 
Cpp :: c++ count vector elements 
Cpp :: how to declare a 2d vector stack 
Cpp :: char array declaration c++ 
Cpp :: find vector size in c++ 
Cpp :: how to concatinate two strings in c++ 
Cpp :: c++ custom hash 
Cpp :: bit++ codeforces in c++ 
Cpp :: iomanip header file in c++ 
Cpp :: maximum subarray leetcode c++ 
Cpp :: c++ awitch statements 
Cpp :: educative 
Cpp :: stl map remove item 
Cpp :: and c++ 
Cpp :: build a prefix array cpp 
Cpp :: raspberry pi mount external hard drive 
Cpp :: char at in c++ 
Cpp :: c++ shared pointer operator bool 
Cpp :: waiting in a serial as the spool reflect the queue operation. Demonstrate Printer Behavior in context of Queue.Subject to the Scenario implement the Pop and Push Using C++. 
Cpp :: 41.00 
Cpp :: c++ solver online free 
Cpp :: C++ Changing Default Value of Enums 
Cpp :: c++ bind port 
Cpp :: progress bar custom color c++ buider 
ADD CONTENT
Topic
Content
Source link
Name
8+8 =