Convert bool array to int32 ,unsigned int and double?

[亡魂溺海] 提交于 2019-12-10 11:58:03

问题


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)? for example :

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

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



回答2:


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



回答3:


#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


来源:https://stackoverflow.com/questions/32410186/convert-bool-array-to-int32-unsigned-int-and-double

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!