How to convert decimal to binary in C++

2 Answers

0 votes
#include <iostream>

int main(void) {
    int dec = 253, bin[16], i = 0;

    while(dec != 0) {
        bin[i] = dec % 2;
        i++;
        dec /= 2;
    }
    for(i = i - 1; i >= 0; i--)
        std::cout << bin[i];
        
    return 0;        
}



  
/*
run:
   
11111101
   
*/

 



answered Aug 24, 2021 by avibootz
0 votes
#include <iostream>
#include <bitset>

int main(void) {
    std::string binary = std::bitset<16>(253).to_string(); 
    
    std::cout << binary;

    return 0;        
}



  
/*
run:
   
0000000011111101
   
*/

 



answered Aug 24, 2021 by avibootz

Related questions

2 answers 179 views
1 answer 131 views
131 views asked Aug 24, 2021 by avibootz
1 answer 148 views
1 answer 220 views
1 answer 148 views
2 answers 187 views
1 answer 115 views
115 views asked Aug 24, 2021 by avibootz
...