图像遮盖掩码 c语言,C语言高效的数组掩码

我的评论中提到这一点,但这里的一些工作的代码(希望我没有测试这个我也没有给它通过一个编译器。这只是想法)。如果你有你想建模为一个位掩码数组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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值