将bool数组转换为int32,unsigned int和double?

时间:2015-09-05 06:24:44

标签: c++ algorithm data-conversion boolean-operations

我有大小的bool数组:32,48,64(每个布尔代表一点)。如何将它们转换为性能良好的数字(int,unsigned int,double48,double64)? 例如:

bool ar[] = {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);// num = 65

3 个答案:

答案 0 :(得分:1)

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);
}

答案 1 :(得分:0)

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;
}

输出:

47
255
32767
18446744073709551615

答案 2 :(得分:0)

#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