c++performancealgorithmbytebit

Best way to convert 8 boolean to one byte?


I want to save 8 boolean to one byte and then save it to a file(this work must be done for a very large data), I've used the following code but I'm not sure it is the best one(in terms of speed and space):

int bits[]={1,0,0,0,0,1,1,1};
char a='\0';
for (int i=0;i<8;i++){
  a=a<<1;
  a+=bits[i]
}
//and then save "a"

can anyone give me a better code(more speed) ?


Solution

  • If you don't mind using SSE intrinsics, then _mm_movemask_epi8 is an excellent fit. It uses 16 bytes, but you can just set the others to zero.

    For example (not tested)

    __m128i values = _mm_loadl_epi64((__m128i*)array);
    __m128i order = _mm_set_epi8(0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
                                 0, 1, 2, 3, 4, 5, 6, 7);
    values = _mm_shuffle_epi8(values, order);
    int result = _mm_movemask_epi8(_mm_slli_epi32(values, 7));
    

    This assumes the array is an array of chars. If you can't make that happen, it takes some more loads and packs and it becomes a bit annoying.