2015-09-05 81 views
0

我布爾大小的數組:32,48,64(每個布爾表示一點)。如何將它們轉換爲具有良好性能的數字(int,unsigned int,double48,double64)? 例如:將bool數組轉換爲int32,unsigned int和double?

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 
+0

你究竟是什麼意思_convert_?對於整數,所有的位都可以從一個循環移入,但我不確定你想要的是什麼。它們的位表示是在編譯器中定義的實現。 –

回答

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