Changing integer to binary string of digits

后端 未结 5 1020
梦毁少年i
梦毁少年i 2020-12-15 04:24

I\'m currently working on a simulation of the MIPS processor in C++ for a comp architecture class and having some problems converting from decimal numbers to binary (signed

5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-15 04:53

    There are actually standard one-liners for these.

    #include 
    
    std::string s = std::bitset< 64 >( 12345 ).to_string(); // string conversion
    
    std::cout << std::bitset< 64 >( 54321 ) << ' '; // direct output
    
    std::bitset< 64 > input;
    std::cin >> input;
    unsigned long ul = input.to_ulong();
    

    See this run as a demo.

提交回复
热议问题