Converting integer to a bit representation

后端 未结 6 2058
死守一世寂寞
死守一世寂寞 2020-12-09 11:30

How can I convert a integer to its bit representation. I want to take an integer and return a vector that has contains 1\'s and 0\'s of the integer\'s bit representation.

6条回答
  •  情书的邮戳
    2020-12-09 12:03

    The world's worst integer to bit as bytes converter:

    #include 
    #include 
    #include 
    #include 
    
    class zero_ascii_iterator: public std::iterator
    {
    public:
        zero_ascii_iterator &operator++()
        {
            return *this;
        }
    
        char operator *() const
        {
            return '0';
        }
    };
    
    
    char bits[33];
    
    _itoa(value, bits, 2);
    std::transform(
        bits, 
        bits + strlen(bits), 
        zero_ascii_iterator(), 
        bits, 
        std::minus());
    

提交回复
热议问题