read raw binary file in to bitset vector

2019-08-18 04:08发布

问题:

Am I forced to read the file into a string and then convert the string to bitset?

I have 8192 bits stored in a file and I need to get the nth bit of every 256 bit set. My idea is to load all the bits into a std::vector<std::bitset> then use boost to myvector | boost::adaptors::strided(64) Is there a better way to do this? This was really easy to implement with python using the bitstring module but it was really slow. Trying to do it in C++ but running into all sorts of issues that I'm not familiar with.

Python code (this also switches the endianess of the byte) Here ch is 32, 32 byte arrays. The outer loops selects the large element and the inner loop reorders the byte.:

jrange = range(32)
irange = range(8) 
for j in jrange:
                for i in irange:
                    (BitArray([element[(7-i)+j*8] for element in ch])).tofile(outfile)