bitvec icon indicating copy to clipboard operation
bitvec copied to clipboard

The best way to merge multiple bit vectors?

Open Anivie opened this issue 1 year ago • 1 comments

I am trying to merge multiple BitVecs, such that if a position has a 1 in one of the BitVecs, that position in the final set will store 1; if all the BitVecs have 0 in that position, the result will be 0. I am attempting to accomplish this task in the following way:

for each_class in mask {
    for all in mask_all.as_raw_mut_slice() {
        for current in each_class.mask.as_raw_slice() {
            *all |= *current;
        }
    }
}

But it doesn't seem to be working properly, how should I change it? This is the current version that works:

for each_class in mask {
    for (index, all) in each_class.mask.iter().enumerate() {
        if *all {
            mask_all.set(index, true);
        }
    }
}

Anivie avatar Sep 29 '24 09:09 Anivie

I know this thread is old, but this should work (or am I missing something?):

for each_class in mask {
    mask_all |= each_class;
}

ghcs27 avatar Jun 17 '25 11:06 ghcs27