samedi 27 février 2016

Data stucture for large binary data manipulation

Hello fellow programmers,

I am working on a genetic project where the speed efficiency is crucial. I have essentially a lot of binary data to process. I work in c++11. I have two functions that needs to be optimized.

First, I need to be able to use binary operators between four binary strings and then check if all the bits are all zeros.

i.e. (bitV1 & (bitV2^bitV3)| bitV4) == 0..0

Second, I sometimes need to flip certain bits at certain location.

My problem is that bitset need to know the size at compile time, and I don’t know that size at compile time only at execution. And vector doesn’t seem to work with binary operators. I could translate my data in chars/string or int arrays and then use the bitwises operators on those but the code is not going to be pretty.

Does somebody know an efficient and simple way to do this?

Here is an MWE:

#include<iostream>
#include<bitset>
#include<vector>


int main() {
// I dont know the sizes for the sequences at compile time.
//std::bitset<intFromFile> firstBitset ("0011"); // doesnt compile

std::bitset<4> firstBitset ("0011");
std::bitset<4> secondBitset ("0101");
std::bitset<4> resultBitset = firstBitset &secondBitset;
std::cout << resultBitset; //OK

std::vector<bool> firstVector  {0,0,1,1};
std::vector<bool> secondVector {0,1,0,1};
//std::vector<bool> result = firstVector^secondVector; //ERROR
std::vector<bool> result {0,0,0,1}; //  OK

for (unsigned short int i = 0 ; i < result.size(); ++i){
    std::cout << result.at(i);
}
std::cout << std::endl;

return 0;
}

Aucun commentaire:

Enregistrer un commentaire