BitVecHammingDistance
Description
Calculate Hamming distance between two bitvectors. Hamming distance is the number of positions where bits differ.
Parameters
| Name | Direction | Description |
|---|---|---|
bv1 |
in | First bitvector |
bv2 |
in | Second bitvector |
Usage example (from documentation)
u64 distance = BitVecHammingDistance(&bv1, &bv2);Success
Returns the number of bit positions at which bv1 and bv2 differ. Bits beyond the shorter operand are treated as 0. Neither operand is modified.
Failure
Function cannot fail.
Usage example (Cross-references)
Usage examples (Cross-references)
- In
BitVec.c:1402:
}
u64 BitVecHammingDistance(BitVec *bv1, BitVec *bv2) {
ValidateBitVec(bv1);
ValidateBitVec(bv2);- In
BitVec.Math.c:54:
DefaultAllocator alloc = DefaultAllocatorInit();
WriteFmt("Testing BitVecHammingDistance basic functionality\n");
BitVec bv1 = BitVecInit(ALLOCATOR_OF(&alloc));- In
BitVec.Math.c:68:
BitVecPush(&bv2, true);
u64 distance = BitVecHammingDistance(&bv1, &bv2);
result = result && (distance == 0);- In
BitVec.Math.c:77:
BitVecPush(&bv2, false);
distance = BitVecHammingDistance(&bv1, &bv2);
result = result && (distance == 3);- In
BitVec.Math.c:86:
BitVecPush(&bv2, true); // Same
distance = BitVecHammingDistance(&bv1, &bv2);
result = result && (distance == 1);- In
BitVec.Math.c:99:
DefaultAllocator alloc = DefaultAllocatorInit();
WriteFmt("Testing BitVecHammingDistance edge cases\n");
BitVec bv1 = BitVecInit(ALLOCATOR_OF(&alloc));
// Test empty bitvectors
u64 distance = BitVecHammingDistance(&bv1, &bv2);
result = result && (distance == 0); BitVecPush(&bv2, true);
distance = BitVecHammingDistance(&bv1, &bv2);
result = result && (distance == 1); // 1 length difference
// Test one empty, one non-empty
BitVecClear(&bv2);
distance = BitVecHammingDistance(&bv1, &bv2);
result = result && (distance == 2); // Length of bv1
// Test that all functions complete without crashing
u64 hamming = BitVecHammingDistance(&bv1, &bv2);
double jaccard = BitVecJaccardSimilarity(&bv1, &bv2);
double cosine = BitVecCosineSimilarity(&bv1, &bv2); DefaultAllocator alloc = DefaultAllocatorInit();
WriteFmt("Testing BitVecHammingDistance(NULL, bv2) - should fatal\n");
BitVec bv2 = BitVecInit(ALLOCATOR_OF(&alloc));
BitVecPush(&bv2, true); BitVec bv2 = BitVecInit(ALLOCATOR_OF(&alloc));
BitVecPush(&bv2, true);
BitVecHammingDistance(NULL, &bv2);
BitVecDeinit(&bv2);
DefaultAllocatorDeinit(&alloc); DefaultAllocator alloc = DefaultAllocatorInit();
WriteFmt("Testing BitVecHammingDistance(bv1, NULL) - should fatal\n");
BitVec bv1 = BitVecInit(ALLOCATOR_OF(&alloc));
BitVecPush(&bv1, true); BitVec bv1 = BitVecInit(ALLOCATOR_OF(&alloc));
BitVecPush(&bv1, true);
BitVecHammingDistance(&bv1, NULL);
BitVecDeinit(&bv1);
DefaultAllocatorDeinit(&alloc);
Last updated on