我的评论中提到这一点,但这里的一些工作的代码(希望我没有测试这个我也没有给它通过一个编译器。这只是想法)。如果你有你想建模为一个位掩码数组100x100x100,那么你就可以做到以下几点:
// Create two bitmasks
const unsigned int BITS_PER_BYTE = 8;
const unsigned int DIM = 100;
const unsigned int BITS_PER_VALUE = BITS_PER_BYTE * sizeof(unsigned long);
const unsigned long MASK_SIZE = (DIM * DIM * DIM)/BITS_PER_VALUE;
unsigned long bitmask1[MASK_SIZE] = {0};
unsigned long bitmask2[MASK_SIZE] = {0};
unsigned long bitmask_result[MASK_SIZE];
// Set the two bitmasks, this is probably sub-optimal but you
// mention that setting bitmasks isn't supposed to be overly performant
// set bitmask1 (repeat something similar for bitmask2)
for (int i = 0; i < DIM; ++i)
for (int j = 0; j < DIM; ++j)
for (int k = 0; k < DIM; ++k) {
// set bitmask[i][j][k] to 1
unsigned int offset = DIM*DIM*i + DIM*j + k;
unsigned int long_offset = offset/BITS_PER_VALUE;
unsigned int bit_offset = offset % BITS_PER_VALUE;
// XXX SET THIS TO WHATEVER VALUE YOU HAVE, 1 FOR true and 0
// FOR false. I'M SETTING EVERYTHING TO TRUE FOR THE SAKE OF
// EXAMPLE
bitmask1[long_offset] = 1 << bit_offset;
}
// Now to actually compare:
for (int i = 0; i < MASK_SIZE; ++i) {
bitmask_result[i] = bitmask1[i] & bitmask2[i];
// and that's it. bitmask_result will now have your answers. decompose
// the bitmask by doing the reverse of the above set loop