1

Given the following code, how would I do a bitwise And on the 2 bitmaps and get the resulting bitmap back?

    CFBitVectorRef maskForCollection = CFBitVectorCreate(nil, 0, _tables.AllObjects.count);

    for ( LocalFilter * filter in self)
    {
        CFBitVectorRef maskForFilter = filter.ValidObjectMask;

        CFBitVectorRef blah = maskForCollection & maskForFilter;
    }

This post is relevant: How to bitwise-and CFBitVector, but it failed to help at all...

Community
  • 1
  • 1
Darthg8r
  • 12,377
  • 15
  • 63
  • 100
  • possible duplicate of [How to bitwise-and CFBitVector](http://stackoverflow.com/questions/9846516/how-to-bitwise-and-cfbitvector) – Sven Jan 26 '13 at 09:35
  • Did you read the end of my question Sven? I acknowledged that, but I need further help... – Darthg8r Jan 26 '13 at 14:20
  • Do all bitmaps have the same length? – Martin R Jan 26 '13 at 18:44
  • Yes, both will be the same length. – Darthg8r Jan 26 '13 at 19:51
  • @Darthg8r while I agree the answer in that question is not great, you should probably specify what exactly didn't work about that question's answer, it will help differentiate your question and give context to answerers. – Carl Veazey Jan 26 '13 at 20:51
  • @Darthg8r: If an answer helped, you can "accept" it by clicking on the check mark. Otherwise feel free to ask for more information. – Martin R Feb 02 '13 at 09:28

1 Answers1

2

The following function creates a CFBitVectorRef that is the bitwise AND of two bitmaps. The function assumes that both bitmaps have the same size count (which is true in your case, as you said in a comment).

The function creates a (mutable) copy of the first bitmap and then clears all bits that are zero in the second bitmap.

CFBitVectorRef CreateBitmapAnd(CFBitVectorRef bm1, CFBitVectorRef bm2, CFIndex count)
{
    CFMutableBitVectorRef result = CFBitVectorCreateMutableCopy(NULL, count, bm1);
    for (CFIndex i = 0; i < count; i++) {
        if (CFBitVectorGetBitAtIndex(bm2, i) == 0)
            CFBitVectorSetBitAtIndex(result, i, 0);
    }
    return result;
}

In contrast to the proposed solution to How to bitwise-and CFBitVector, this function does not use the internal representation of bitmaps.

A different implementation is the following:

CFBitVectorRef CreateBitmapAnd(CFBitVectorRef bm1, CFBitVectorRef bm2, CFIndex count)
{
    size_t nbytes = (count + 7)/8;
    UInt8 *bytes1 = malloc(nbytes); // (Error checking omitted)
    UInt8 *bytes2 = malloc(nbytes);
    CFBitVectorGetBits(bm1, CFRangeMake(0, count), bytes1);
    CFBitVectorGetBits(bm2, CFRangeMake(0, count), bytes2);
    for (size_t i = 0; i < nbytes; i++) {
        bytes1[i] &= bytes2[i];
    }
    CFBitVectorRef result = CFBitVectorCreate(NULL, bytes1, count);
    free(bytes1);
    free(bytes2);
    return result;
}

The advantage is that it operates on bytes instead of bits (and this could be further optimized to operate on ints), the disadvantage is that memory needs to be allocated and freed.

Community
  • 1
  • 1
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382