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 152 views
1 answer 206 views
1 answer 215 views
1 answer 288 views
1 answer 226 views
1 answer 202 views
1 answer 148 views
...