C++ - Decimal to binary converting

后端 未结 29 3574
一向
一向 2020-11-28 18:53

I wrote a \'simple\' (it took me 30 minutes) program that converts decimal number to binary. I am SURE that there\'s a lot simpler way so can you show me? Here\'s the code:<

29条回答
  •  执笔经年
    2020-11-28 19:39

    Here is modern variant that can be used for ints of different sizes.

    #include 
    #include 
    
    template
    std::enable_if_t,std::string>
    encode_binary(T i){
        return std::bitset(i).to_string();
    }
    

提交回复
热议问题