How to convert int to binary and remove leading zeros in C++

1 Answer

0 votes
#include <iostream>
#include <bitset>
 
int main()
{
    unsigned int num = 153;
    
    std::string binary = std::bitset<16>(num).to_string(); 
    binary.erase(0, binary.find_first_not_of('0'));
    
    std::cout << binary;
}
 
   
   
   
/*
run:
   
10011001
   
*/

 



answered Jan 7, 2024 by avibootz

Related questions

1 answer 145 views
1 answer 195 views
1 answer 202 views
1 answer 280 views
1 answer 222 views
1 answer 195 views
1 answer 142 views
...