Convert bool array to int32 ,unsigned int and double?

后端 未结 3 1421
甜味超标
甜味超标 2021-01-17 00:14

I\'ve bool arrays of sizes : 32, 48, 64 (each boolean represents a bit). how can I convert them to a number with a good performance( int, unsigned int, double48, double64)?

相关标签:
3条回答
  • 2021-01-17 00:45

    O(n):

    int bitArrayToInt32(bool arr[], int count)
    {
        int ret = 0;
        int tmp;
        for (int i = 0; i < count; i++) {
            tmp = arr[i];
            ret |= tmp << (count - i - 1);
        }
        return ret;
    }
    
    int main()
    {
        bool ar[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1};
        int num = bitArrayToInt32(ar,32);
        printf("number = %d\n", num);
    }
    
    0 讨论(0)
  • 2021-01-17 00:52
    #include <iostream>
    #include <bitset>
    #include <climits>
    
    enum class endianness : bool { big, little };
    
    constexpr endianness getEndianness()
    {
        uint32_t word = 1;
        uint8_t* byte = (uint8_t*)& word;
    
        if(byte[0])
            return endianness::little;
        else
            return endianness::big;
    }
    
    constexpr bool isLittleEndian()
    {
        switch(getEndianness())
        {
            case endianness::little:
                return true;
            case endianness::big:
                return false;
        }
    }
    
    auto make_bitSet (bool flags[], size_t size)
    {
        std::bitset< 8*sizeof(ULLONG_MAX) > bitSet;
    
        if( isLittleEndian() )
            for (size_t i = 0; i < size; ++i)
                bitSet.set( i, flags[size-i-1] );
        else
            for (size_t i = 0; i < size; ++i)
                bitSet.set( i, flags[i] );
    
        return bitSet.to_ullong();
    }
    
    int main (int, char**)
    {
        bool ar1[] = {1, 0, 1, 0            };
        bool ar2[] = {1, 0, 1, 0, 1, 1, 0, 0};
        bool ar3[] = {1, 0, 1, 0, 1, 1, 0, 0,
                      1, 0, 1, 0, 1, 1, 0, 0};
        bool ar4[] = {1, 0, 1, 0, 1, 1, 0, 0,
                      1, 0, 1, 0, 1, 1, 0, 0,
                      1, 0, 1, 0, 1, 1, 0, 0,
                      1, 0, 1, 0, 1, 1, 0, 0,
                      1, 0, 1, 0, 1, 1, 0, 0,
                      1, 0, 1, 0, 1, 1, 0, 0};
        bool ar5[] = {1, 0, 1, 0, 1, 1, 0, 0,
                      1, 0, 1, 0, 1, 1, 0, 0,
                      1, 0, 1, 0, 1, 1, 0, 0,
                      1, 0, 1, 0, 1, 1, 0, 0,
                      1, 0, 1, 0, 1, 1, 0, 0,
                      1, 0, 1, 0, 1, 1, 0, 0,
                      1, 0, 1, 0, 1, 1, 0, 0,
                      1, 0, 1, 0, 1, 1, 0, 0};
    
        std::cout << make_bitSet(ar1,  4) << '\n';
        std::cout << make_bitSet(ar2,  8) << '\n';
        std::cout << make_bitSet(ar3, 16) << '\n';
        std::cout << make_bitSet(ar4, 48) << '\n';
        std::cout << make_bitSet(ar5, 64) << std::endl;
    
        return 0;
    }
    

    cout

    10
    172
    44204
    189857631349932
    12442509728149187756
    
    0 讨论(0)
  • 2021-01-17 00:54
    template <typename T>
    T make_number (bool ar[], size_t ar_size) {
        T ret {};
    
        for (size_t i = 0; i < ar_size; ++i) {
            T s {ar[i]};
            s <<= i;
            ret |= s;
        }
    
        return ret;
    }
    
    
    int main (int, char**) {
    
    
        bool ar1[] = {1, 1, 1, 1, 0, 1};
        bool ar2[] = {1, 1, 1, 1, 1, 1, 1, 1};
        bool ar3[] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0};
        bool ar4[] = {1, 1, 1, 1, 1, 1, 1, 1,
                      1, 1, 1, 1, 1, 1, 1, 1,
                      1, 1, 1, 1, 1, 1, 1, 1,
                      1, 1, 1, 1, 1, 1, 1, 1,
                      1, 1, 1, 1, 1, 1, 1, 1,
                      1, 1, 1, 1, 1, 1, 1, 1,
                      1, 1, 1, 1, 1, 1, 1, 1,
                      1, 1, 1, 1, 1, 1, 1, 1};
    
        cout << static_cast<int> (make_number<char> (ar1, 6)) << endl;
        cout << static_cast<int> (make_number<unsigned char> (ar2, 8)) << endl;
        cout << make_number<short> (ar3, 16) << endl;
        cout << make_number<uint64_t> (ar4, 64) << endl;
    
        return 0;
    }
    

    And the output:

    47
    255
    32767
    18446744073709551615
    
    0 讨论(0)
提交回复
热议问题