BitVecHammingDistance

Table of Contents

BitVecHammingDistance

Description

Calculate Hamming distance between two bitvectors. Hamming distance is the number of positions where bits differ.

Parameters

NameDirectionDescription
bv1inFirst bitvector
bv2inSecond bitvector

Usage example (from documentation)

  u64 distance = BitVecHammingDistance(&bv1, &bv2);

Usage example (Cross-references)

    // Math functions implementation
    
    u64 BitVecHammingDistance(BitVec *bv1, BitVec *bv2) {
    ValidateBitVec(bv1);
    ValidateBitVec(bv2);
    // Test BitVecHammingDistance basic functionality
    bool test_bitvec_hamming_distance_basic(void) {
    printf("Testing BitVecHammingDistance basic functionality\n");
    
    BitVec bv1    = BitVecInit();
    BitVecPush(&bv2, true);
    
    u64 distance = BitVecHammingDistance(&bv1, &bv2);
    result       = result && (distance == 0);
    BitVecPush(&bv2, false);
    
    distance = BitVecHammingDistance(&bv1, &bv2);
    result   = result && (distance == 3);
    BitVecPush(&bv2, true); // Same
    
    distance = BitVecHammingDistance(&bv1, &bv2);
    result   = result && (distance == 1);
    // Test BitVecHammingDistance edge cases
    bool test_bitvec_hamming_distance_edge_cases(void) {
    printf("Testing BitVecHammingDistance edge cases\n");
    
    BitVec bv1    = BitVecInit();
    
    // 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);
    
    bool test_bitvec_hamming_distance_null_bv1(void) {
    printf("Testing BitVecHammingDistance(NULL, bv2) - should fatal\n");
    BitVec bv2 = BitVecInit();
    BitVecPush(&bv2, true);
    BitVec bv2 = BitVecInit();
    BitVecPush(&bv2, true);
    BitVecHammingDistance(NULL, &bv2);
    BitVecDeinit(&bv2);
    return true;
    
    bool test_bitvec_hamming_distance_null_bv2(void) {
    printf("Testing BitVecHammingDistance(bv1, NULL) - should fatal\n");
    BitVec bv1 = BitVecInit();
    BitVecPush(&bv1, true);
    BitVec bv1 = BitVecInit();
    BitVecPush(&bv1, true);
    BitVecHammingDistance(&bv1, NULL);
    BitVecDeinit(&bv1);
    return true;

Share :

Related Posts

BitVecRemoveAll

BitVecRemoveAll Description Remove all occurrences of a specific bit value. Returns the number of bits that were removed.

Read More

BitVecRemoveRange

BitVecRemoveRange Description Remove multiple consecutive bits starting at a specific position. All bits after the removed range are shifted left.

Read More

ALIGN_DOWN

ALIGN_DOWN Description Aligns the given value down to the nearest multiple of alignment.

Read More