Skip to content

BitVecGet

BitVecGet

Description

Get the value of bit at given index in bitvector.

Parameters

Name Direction Description
bv in Bitvector to get bit from
idx in Index of bit to get (0-based)

Usage example (from documentation)

  bool flag = BitVecGet(&flags, 5);

Returns

true if bit is set, false otherwise

Usage example (Cross-references)

Usage examples (Cross-references)
    
        for (u64 i = value->bits.length; i > 0; i--) {
            if (BitVecGet(INT_BITS(value), i - 1)) {
                return i;
            }
    static bool int_is_odd(Int *value) {
        ValidateInt(value);
        return value->bits.length > 0 && BitVecGet(INT_BITS(value), 0);
    }
    static bool int_is_one(Int *value) {
        ValidateInt(value);
        return IntBitLength(value) == 1 && BitVecGet(INT_BITS(value), 0);
    }
    
        for (u64 i = 0; i < value->bits.length; i++) {
            if (BitVecGet(INT_BITS(value), i)) {
                return i;
            }
                u64 bit_idx = i * 8 + bit;
    
                if (bit_idx < value->bits.length && BitVecGet(INT_BITS(value), bit_idx)) {
                    byte |= (u8)(1u << bit);
                }
                u64 bit_idx = i * 8 + bit;
    
                if (bit_idx < value->bits.length && BitVecGet(INT_BITS(value), bit_idx)) {
                    byte |= (u8)(1u << bit);
                }
    
        for (u64 i = lhs_bits; i > 0; i--) {
            bool lhs_bit = BitVecGet(INT_BITS(lhs), i - 1);
            bool rhs_bit = BitVecGet(INT_BITS(rhs), i - 1);
        for (u64 i = lhs_bits; i > 0; i--) {
            bool lhs_bit = BitVecGet(INT_BITS(lhs), i - 1);
            bool rhs_bit = BitVecGet(INT_BITS(rhs), i - 1);
    
            if (lhs_bit != rhs_bit) {
    
        for (u64 i = bits; i > 0; i--) {
            BitVecSet(INT_BITS(value), i - 1 + positions, BitVecGet(INT_BITS(value), i - 1));
        }
    
        for (u64 i = 0; i + positions < bits; i++) {
            BitVecSet(INT_BITS(value), i, BitVecGet(INT_BITS(value), i + positions));
        }
            u64 sum = carry ? 1u : 0u;
    
            if (i < a_bits && BitVecGet(INT_BITS(a), i)) {
                sum++;
            }
                sum++;
            }
            if (i < b_bits && BitVecGet(INT_BITS(b), i)) {
                sum++;
            }
    
        for (u64 i = 0; i < a_bits; i++) {
            int ai   = (i < a_bits && BitVecGet(INT_BITS(a), i)) ? 1 : 0;
            int bi   = (i < b_bits && BitVecGet(INT_BITS(b), i)) ? 1 : 0;
            int diff = ai - bi - (borrow ? 1 : 0);
        for (u64 i = 0; i < a_bits; i++) {
            int ai   = (i < a_bits && BitVecGet(INT_BITS(a), i)) ? 1 : 0;
            int bi   = (i < b_bits && BitVecGet(INT_BITS(b), i)) ? 1 : 0;
            int diff = ai - bi - (borrow ? 1 : 0);
    
        for (u64 i = 0; i < b_bits; i++) {
            if (!BitVecGet(INT_BITS(b), i)) {
                continue;
            }
        // Copy all bits
        for (u64 i = 0; i < bv->length; i++) {
            bool bit = BitVecGet(bv, i);
            BitVecSet(&clone, i, bit);
        }
    }
    
    bool BitVecGet(BitVec *bitvec, u64 idx) {
        ValidateBitVec(bitvec);
        if (idx >= bitvec->length) {
            LOG_FATAL("Cannot pop from empty bitvector");
        }
        bool value = BitVecGet(bitvec, bitvec->length - 1);
        BitVecResize(bitvec, bitvec->length - 1);
        return value;
        // Shift bits right from idx to end
        for (u64 i = bitvec->length - 1; i > idx; i--) {
            bool bit = BitVecGet(bitvec, i - 1);
            BitVecSet(bitvec, i, bit);
        }
        for (u64 i = old_length; i > idx;) {
            i--;
            bool bit = BitVecGet(bv, i);
            BitVecSet(bv, i + count, bit);
        }
        for (u64 i = old_length; i > idx;) {
            i--;
            bool bit = BitVecGet(bv, i);
            BitVecSet(bv, i + other->length, bit);
        }
        // Insert bits from other bitvector
        for (u64 i = 0; i < other->length; i++) {
            bool bit = BitVecGet(other, i);
            BitVecSet(bv, idx + i, bit);
        }
        for (u64 i = old_length; i > idx;) {
            i--;
            bool bit = BitVecGet(bv, i);
            BitVecSet(bv, i + pattern_bits, bit);
        }
    
        // Get the bit value before removing it
        bool removed_bit = BitVecGet(bv, idx);
    
        // Shift bits left from idx+1 to end
        // Shift bits left from idx+1 to end
        for (u64 i = idx; i < bv->length - 1; i++) {
            bool bit = BitVecGet(bv, i + 1);
            BitVecSet(bv, i, bit);
        }
        // Shift bits left to close the gap
        for (u64 i = idx + count; i < bv->length; i++) {
            bool bit = BitVecGet(bv, i);
            BitVecSet(bv, i - count, bit);
        }
        // Compact the bitvector by copying only bits that don't match the value
        for (u64 read_idx = 0; read_idx < bv->length; read_idx++) {
            bool bit = BitVecGet(bv, read_idx);
            if (bit != value) {
                // Keep this bit
        u64 count = 0;
        for (u64 i = 0; i < bitvec->length; i++) {
            if (BitVecGet(bitvec, i)) {
                count++;
            }
    
        for (u64 i = 0; i < min_len; i++) {
            bool bit_a = BitVecGet(a, i);
            bool bit_b = BitVecGet(b, i);
            BitVecSet(result, i, bit_a && bit_b);
        for (u64 i = 0; i < min_len; i++) {
            bool bit_a = BitVecGet(a, i);
            bool bit_b = BitVecGet(b, i);
            BitVecSet(result, i, bit_a && bit_b);
        }
    
        for (u64 i = 0; i < max_len; i++) {
            bool bit_a = i < a->length ? BitVecGet(a, i) : false;
            bool bit_b = i < b->length ? BitVecGet(b, i) : false;
            BitVecSet(result, i, bit_a || bit_b);
        for (u64 i = 0; i < max_len; i++) {
            bool bit_a = i < a->length ? BitVecGet(a, i) : false;
            bool bit_b = i < b->length ? BitVecGet(b, i) : false;
            BitVecSet(result, i, bit_a || bit_b);
        }
    
        for (u64 i = 0; i < max_len; i++) {
            bool bit_a = i < a->length ? BitVecGet(a, i) : false;
            bool bit_b = i < b->length ? BitVecGet(b, i) : false;
            BitVecSet(result, i, bit_a != bit_b);
        for (u64 i = 0; i < max_len; i++) {
            bool bit_a = i < a->length ? BitVecGet(a, i) : false;
            bool bit_b = i < b->length ? BitVecGet(b, i) : false;
            BitVecSet(result, i, bit_a != bit_b);
        }
    
        for (u64 i = 0; i < bitvec->length; i++) {
            bool bit = BitVecGet(bitvec, i);
            BitVecSet(result, i, !bit);
        }
    
        for (u64 i = 0; i < len; i++) {
            if (BitVecGet(bv1, start1 + i) != BitVecGet(bv2, start2 + i)) {
                return false;
            }
        // Compare bit by bit in the specified range
        for (u64 i = 0; i < len; i++) {
            bool bit1 = BitVecGet(bv1, start1 + i);
            bool bit2 = BitVecGet(bv2, start2 + i);
        for (u64 i = 0; i < len; i++) {
            bool bit1 = BitVecGet(bv1, start1 + i);
            bool bit2 = BitVecGet(bv2, start2 + i);
    
            if (bit1 != bit2) {
        for (u64 i = max_len; i > 0;) {
            i--;
            bool bit1 = i < bv1->length ? BitVecGet(bv1, i) : false;
            bool bit2 = i < bv2->length ? BitVecGet(bv2, i) : false;
            i--;
            bool bit1 = i < bv1->length ? BitVecGet(bv1, i) : false;
            bool bit2 = i < bv2->length ? BitVecGet(bv2, i) : false;
    
            if (bit1 != bit2) {
    
        // Get sign bits (MSB)
        bool sign1 = bv1->length > 0 ? BitVecGet(bv1, bv1->length - 1) : false;
        bool sign2 = bv2->length > 0 ? BitVecGet(bv2, bv2->length - 1) : false;
        // Get sign bits (MSB)
        bool sign1 = bv1->length > 0 ? BitVecGet(bv1, bv1->length - 1) : false;
        bool sign2 = bv2->length > 0 ? BitVecGet(bv2, bv2->length - 1) : false;
    
        // If signs differ, negative < positive
    
        for (u64 i = 0; i < max_len; i++) {
            bool bit1 = i < bv1->length ? BitVecGet(bv1, i) : false;
            bool bit2 = i < bv2->length ? BitVecGet(bv2, i) : false;
        for (u64 i = 0; i < max_len; i++) {
            bool bit1 = i < bv1->length ? BitVecGet(bv1, i) : false;
            bool bit2 = i < bv2->length ? BitVecGet(bv2, i) : false;
    
            // If bv1 has a 1-bit where bv2 has a 0-bit, bv1 is not a subset
    
        for (u64 i = 0; i < min_len; i++) {
            if (BitVecGet(bv1, i) && BitVecGet(bv2, i)) {
                return false; // Found common 1-bit
            }
    
        for (u64 i = 0; i < bv->length; i++) {
            bool bit = BitVecGet(bv, i);
    
            if (bit) {
        // Convert each bit to '0' or '1'
        for (u64 i = 0; i < bv->length; i++) {
            char bit_char = BitVecGet(bv, i) ? '1' : '0';
            StrPushBack(&result, bit_char);
        }
        // Copy bits to bytes
        for (u64 i = 0; i < bv->length && i / 8 < bytes_to_copy; i++) {
            if (BitVecGet(bv, i)) {
                u64 byte_idx     = i / 8;
                u64 bit_offset   = i % 8;
        // Convert bits to integer (LSB first)
        for (u64 i = 0; i < max_bits; i++) {
            if (BitVecGet(bv, i)) {
                result |= (1ULL << i);
            }
        // Shift bits left (move bits to higher indices)
        for (u64 i = bv->length - 1; i >= positions; i--) {
            bool bit = BitVecGet(bv, i - positions);
            BitVecSet(bv, i, bit);
        }
        // Shift bits right (move bits to lower indices)
        for (u64 i = 0; i < bv->length - positions; i++) {
            bool bit = BitVecGet(bv, i + positions);
            BitVecSet(bv, i, bit);
        }
        for (u64 i = 0; i < bv->length; i++) {
            u64  src_idx = (i + positions) % bv->length;
            bool bit     = BitVecGet(&temp, src_idx);
            BitVecSet(bv, i, bit);
        }
        for (u64 i = 0; i < bv->length; i++) {
            u64  src_idx = (i + bv->length - positions) % bv->length;
            bool bit     = BitVecGet(&temp, src_idx);
            BitVecSet(bv, i, bit);
        }
        for (u64 i = 0; i < bv->length / 2; i++) {
            u64  j     = bv->length - 1 - i;
            bool bit_i = BitVecGet(bv, i);
            bool bit_j = BitVecGet(bv, j);
            BitVecSet(bv, i, bit_j);
            u64  j     = bv->length - 1 - i;
            bool bit_i = BitVecGet(bv, i);
            bool bit_j = BitVecGet(bv, j);
            BitVecSet(bv, i, bit_j);
            BitVecSet(bv, j, bit_i);
    
        for (u64 i = 0; i < bv->length; i++) {
            if (BitVecGet(bv, i) == value) {
                return i;
            }
    
        for (u64 i = bv->length - 1; i < bv->length; i--) { // i < bv->length handles underflow
            if (BitVecGet(bv, i) == value) {
                return i;
            }
    
        for (u64 i = 0; i < bv->length; i++) {
            if (BitVecGet(bv, i) != value) {
                return false;
            }
    
        for (u64 i = 0; i < bv->length; i++) {
            if (BitVecGet(bv, i) == value) {
                current_run++;
                if (current_run > max_run) {
        u64  run_count          = 0;
        u64  current_run_length = 1;
        bool current_value      = BitVecGet(bv, 0);
    
        for (u64 i = 1; i < bv->length; i++) {
    
        for (u64 i = 1; i < bv->length; i++) {
            bool bit = BitVecGet(bv, i);
            if (bit == current_value) {
                current_run_length++;
        // Count differences in overlapping region
        for (u64 i = 0; i < min_length; i++) {
            if (BitVecGet(bv1, i) != BitVecGet(bv2, i)) {
                distance++;
            }
    
        for (u64 i = 0; i < max_length; i++) {
            bool bit1 = (i < bv1->length) ? BitVecGet(bv1, i) : false;
            bool bit2 = (i < bv2->length) ? BitVecGet(bv2, i) : false;
        for (u64 i = 0; i < max_length; i++) {
            bool bit1 = (i < bv1->length) ? BitVecGet(bv1, i) : false;
            bool bit2 = (i < bv2->length) ? BitVecGet(bv2, i) : false;
    
            if (bit1 || bit2) {
    
        for (u64 i = 0; i < min_length; i++) {
            if (BitVecGet(bv1, i) && BitVecGet(bv2, i)) {
                product++;
            }
    
            for (u64 j = 1; j <= len2; j++) {
                u64 cost = BitVecGet(bv1, i - 1) == BitVecGet(bv2, j - 1) ? 0 : 1;
    
                u64 deletion     = prev_row[j] + 1;
    
        for (u64 i = 0; i < max_length; i++) {
            double val1 = (i < bv1->length && BitVecGet(bv1, i)) ? 1.0 : 0.0;
            double val2 = (i < bv2->length && BitVecGet(bv2, i)) ? 1.0 : 0.0;
        for (u64 i = 0; i < max_length; i++) {
            double val1 = (i < bv1->length && BitVecGet(bv1, i)) ? 1.0 : 0.0;
            double val2 = (i < bv2->length && BitVecGet(bv2, i)) ? 1.0 : 0.0;
    
            sum1        += val1;
    
        for (u64 i = 0; i < min_length; i++) {
            if (BitVecGet(bv1, i) == BitVecGet(bv2, i)) {
                score += match;
            } else {
    
            for (u64 i = 0; i < bv2->length && (offset + i) < bv1->length; i++) {
                if (BitVecGet(bv1, offset + i) == BitVecGet(bv2, i)) {
                    score++;
                } else {
    
        for (u64 i = 0; i < pattern->length; i++) {
            if (BitVecGet(bv, idx + i) != BitVecGet(pattern, i)) {
                return false;
            }
        // Insert new pattern
        for (u64 i = 0; i < new_pattern->length; i++) {
            BitVecInsert(bv, pos + i, BitVecGet(new_pattern, i));
        }
            // Insert new pattern
            for (u64 i = 0; i < new_pattern->length; i++) {
                BitVecInsert(bv, match_pos + i, BitVecGet(new_pattern, i));
            }
    
        for (u64 i = 0; i < bv->length; i++) {
            if (!BitVecGet(wildcard, i)) { // Not a wildcard position
                if (BitVecGet(bv, i) != BitVecGet(pattern, i)) {
                    return false;
        for (u64 i = 0; i < bv->length; i++) {
            if (!BitVecGet(wildcard, i)) { // Not a wildcard position
                if (BitVecGet(bv, i) != BitVecGet(pattern, i)) {
                    return false;
                }
            u64 errors = 0;
            for (u64 j = 0; j < pattern->length; j++) {
                if (BitVecGet(bv, i + j) != BitVecGet(pattern, j)) {
                    errors++;
                    if (errors > max_errors) {
    // Test BitVecGet function
    bool test_bitvec_get(void) {
        WriteFmt("Testing BitVecGet\n");
    
        BitVec bv = BitVecInit();
    
        // Test getting bits
        bool result = (BitVecGet(&bv, 0) == true) && (BitVecGet(&bv, 1) == false) && (BitVecGet(&bv, 2) == true) &&
                      (BitVecGet(&bv, 3) == false);
        // Test getting bits
        bool result = (BitVecGet(&bv, 0) == true) && (BitVecGet(&bv, 1) == false) && (BitVecGet(&bv, 2) == true) &&
                      (BitVecGet(&bv, 3) == false);
    
        BitVecDeinit(&bv);
    
        // Test getting the set bits
        bool result = (BitVecGet(&bv, 0) == true) && (BitVecGet(&bv, 1) == false) && (BitVecGet(&bv, 2) == true) &&
                      (BitVecGet(&bv, 3) == false);
        // Test getting the set bits
        bool result = (BitVecGet(&bv, 0) == true) && (BitVecGet(&bv, 1) == false) && (BitVecGet(&bv, 2) == true) &&
                      (BitVecGet(&bv, 3) == false);
    
        BitVecDeinit(&bv);
    
        // Test the flipped bits
        bool result = (BitVecGet(&bv, 0) == false) && // was true, now false
                      (BitVecGet(&bv, 1) == true) &&  // was false, now true
                      (BitVecGet(&bv, 2) == true) &&  // unchanged
        // Test the flipped bits
        bool result = (BitVecGet(&bv, 0) == false) && // was true, now false
                      (BitVecGet(&bv, 1) == true) &&  // was false, now true
                      (BitVecGet(&bv, 2) == true) &&  // unchanged
                      (BitVecGet(&bv, 3) == false);   // unchanged
        bool result = (BitVecGet(&bv, 0) == false) && // was true, now false
                      (BitVecGet(&bv, 1) == true) &&  // was false, now true
                      (BitVecGet(&bv, 2) == true) &&  // unchanged
                      (BitVecGet(&bv, 3) == false);   // unchanged
                      (BitVecGet(&bv, 1) == true) &&  // was false, now true
                      (BitVecGet(&bv, 2) == true) &&  // unchanged
                      (BitVecGet(&bv, 3) == false);   // unchanged
    
        BitVecDeinit(&bv);
    // Edge case tests for BitVecGet
    bool test_bitvec_get_edge_cases(void) {
        WriteFmt("Testing BitVecGet edge cases\n");
    
        BitVec bv     = BitVecInit();
        // Test with single bit
        BitVecPush(&bv, true);
        result = result && (BitVecGet(&bv, 0) == true);
    
        // Test with larger index
        }
    
        result = result && (BitVecGet(&bv, 63) == false); // 63 % 2 == 1, so i%2==0 is false for i=63
    
        BitVecDeinit(&bv);
        BitVecResize(&bv, 1);
        BitVecSet(&bv, 0, true);
        bool result = (BitVecGet(&bv, 0) == true);
    
        // Set same bit to false
        // Set same bit to false
        BitVecSet(&bv, 0, false);
        result = result && (BitVecGet(&bv, 0) == false);
    
        BitVecDeinit(&bv);
        BitVecPush(&bv, false);
        BitVecFlip(&bv, 0);
        bool result = (BitVecGet(&bv, 0) == true);
    
        // Flip it again
        // Flip it again
        BitVecFlip(&bv, 0);
        result = result && (BitVecGet(&bv, 0) == false);
    
        BitVecDeinit(&bv);
    
        // Verify pattern: T F T F T
        result = result && (BitVecGet(&bv, 0) == true);
        result = result && (BitVecGet(&bv, 1) == false);
        result = result && (BitVecGet(&bv, 2) == true);
        // Verify pattern: T F T F T
        result = result && (BitVecGet(&bv, 0) == true);
        result = result && (BitVecGet(&bv, 1) == false);
        result = result && (BitVecGet(&bv, 2) == true);
        result = result && (BitVecGet(&bv, 3) == false);
        result = result && (BitVecGet(&bv, 0) == true);
        result = result && (BitVecGet(&bv, 1) == false);
        result = result && (BitVecGet(&bv, 2) == true);
        result = result && (BitVecGet(&bv, 3) == false);
        result = result && (BitVecGet(&bv, 4) == true);
        result = result && (BitVecGet(&bv, 1) == false);
        result = result && (BitVecGet(&bv, 2) == true);
        result = result && (BitVecGet(&bv, 3) == false);
        result = result && (BitVecGet(&bv, 4) == true);
        result = result && (BitVecGet(&bv, 2) == true);
        result = result && (BitVecGet(&bv, 3) == false);
        result = result && (BitVecGet(&bv, 4) == true);
    
        // Count and verify
    
        // Verify some positions
        result = result && (BitVecGet(&bv, 0) == true);    // 0 % 2 == 0
        result = result && (BitVecGet(&bv, 1) == false);   // 1 % 2 != 0
        result = result && (BitVecGet(&bv, 500) == true);  // 500 % 2 == 0
        // Verify some positions
        result = result && (BitVecGet(&bv, 0) == true);    // 0 % 2 == 0
        result = result && (BitVecGet(&bv, 1) == false);   // 1 % 2 != 0
        result = result && (BitVecGet(&bv, 500) == true);  // 500 % 2 == 0
        result = result && (BitVecGet(&bv, 999) == false); // 999 % 2 != 0
        result = result && (BitVecGet(&bv, 0) == true);    // 0 % 2 == 0
        result = result && (BitVecGet(&bv, 1) == false);   // 1 % 2 != 0
        result = result && (BitVecGet(&bv, 500) == true);  // 500 % 2 == 0
        result = result && (BitVecGet(&bv, 999) == false); // 999 % 2 != 0
        result = result && (BitVecGet(&bv, 1) == false);   // 1 % 2 != 0
        result = result && (BitVecGet(&bv, 500) == true);  // 500 % 2 == 0
        result = result && (BitVecGet(&bv, 999) == false); // 999 % 2 != 0
    
        // Verify counts
        BitVecSet(&bv, 1, true);
    
        result = result && (BitVecGet(&bv, 0) == false);
        result = result && (BitVecGet(&bv, 1) == true);
    
        result = result && (BitVecGet(&bv, 0) == false);
        result = result && (BitVecGet(&bv, 1) == true);
    
        // Test count operations
        // Verify pattern
        for (int i = 0; i < size && result; i++) {
            result = result && (BitVecGet(&bv, i) == (i % 3 == 0));
        }
                expected = !expected; // Flipped
            }
            result = result && (BitVecGet(&bv, i) == expected);
        }
        for (int i = 0; i < 100 && result; i++) {
            bool expected = (i * 17 + 3) % 7 < 3;
            result        = result && (BitVecGet(&bv, i) == expected);
        }
    
        // Test NULL bitvec pointer - should abort
        BitVecGet(NULL, 0);
    
        return false;
    
        // Test get from empty bitvec - should abort
        BitVecGet(&bv, 0);
    
        BitVecDeinit(&bv);
    
        // Test with index way beyond length (3) - should abort
        BitVecGet(&bv, 1000);
    
        BitVecDeinit(&bv);
    
        // Test with maximum possible index value - should abort
        BitVecGet(&bv, SIZE_MAX);
    
        BitVecDeinit(&bv);
        // Check result
        bool result = (popped == true) && (bv.length == 2);
        result      = result && (BitVecGet(&bv, 0) == true);
        result      = result && (BitVecGet(&bv, 1) == false);
        bool result = (popped == true) && (bv.length == 2);
        result      = result && (BitVecGet(&bv, 0) == true);
        result      = result && (BitVecGet(&bv, 1) == false);
    
        // Pop another bit
        popped = BitVecPop(&bv);
        result = result && (popped == false) && (bv.length == 1);
        result = result && (BitVecGet(&bv, 0) == true);
    
        // Pop the last bit
        // Check result: true, false, false, true
        bool result = (removed == true) && (bv.length == 4);
        result      = result && (BitVecGet(&bv, 0) == true);
        result      = result && (BitVecGet(&bv, 1) == false);
        result      = result && (BitVecGet(&bv, 2) == false);
        bool result = (removed == true) && (bv.length == 4);
        result      = result && (BitVecGet(&bv, 0) == true);
        result      = result && (BitVecGet(&bv, 1) == false);
        result      = result && (BitVecGet(&bv, 2) == false);
        result      = result && (BitVecGet(&bv, 3) == true);
        result      = result && (BitVecGet(&bv, 0) == true);
        result      = result && (BitVecGet(&bv, 1) == false);
        result      = result && (BitVecGet(&bv, 2) == false);
        result      = result && (BitVecGet(&bv, 3) == true);
        result      = result && (BitVecGet(&bv, 1) == false);
        result      = result && (BitVecGet(&bv, 2) == false);
        result      = result && (BitVecGet(&bv, 3) == true);
    
        // Remove bit at index 0 (first bit)
        removed = BitVecRemove(&bv, 0);
        result  = result && (removed == true) && (bv.length == 3);
        result  = result && (BitVecGet(&bv, 0) == false);
        result  = result && (BitVecGet(&bv, 1) == false);
        result  = result && (BitVecGet(&bv, 2) == true);
        result  = result && (removed == true) && (bv.length == 3);
        result  = result && (BitVecGet(&bv, 0) == false);
        result  = result && (BitVecGet(&bv, 1) == false);
        result  = result && (BitVecGet(&bv, 2) == true);
        result  = result && (BitVecGet(&bv, 0) == false);
        result  = result && (BitVecGet(&bv, 1) == false);
        result  = result && (BitVecGet(&bv, 2) == true);
    
        // Clean up
        // Check result: true, false, true (removed false, true, true)
        bool result = (bv.length == 3);
        result      = result && (BitVecGet(&bv, 0) == true);
        result      = result && (BitVecGet(&bv, 1) == false);
        result      = result && (BitVecGet(&bv, 2) == true);
        bool result = (bv.length == 3);
        result      = result && (BitVecGet(&bv, 0) == true);
        result      = result && (BitVecGet(&bv, 1) == false);
        result      = result && (BitVecGet(&bv, 2) == true);
        result      = result && (BitVecGet(&bv, 0) == true);
        result      = result && (BitVecGet(&bv, 1) == false);
        result      = result && (BitVecGet(&bv, 2) == true);
    
        // Clean up
        // Check result: true, true, false, true (removed first false at index 1)
        bool result = (found == true) && (bv.length == 4);
        result      = result && (BitVecGet(&bv, 0) == true);
        result      = result && (BitVecGet(&bv, 1) == true);
        result      = result && (BitVecGet(&bv, 2) == false);
        bool result = (found == true) && (bv.length == 4);
        result      = result && (BitVecGet(&bv, 0) == true);
        result      = result && (BitVecGet(&bv, 1) == true);
        result      = result && (BitVecGet(&bv, 2) == false);
        result      = result && (BitVecGet(&bv, 3) == true);
        result      = result && (BitVecGet(&bv, 0) == true);
        result      = result && (BitVecGet(&bv, 1) == true);
        result      = result && (BitVecGet(&bv, 2) == false);
        result      = result && (BitVecGet(&bv, 3) == true);
        result      = result && (BitVecGet(&bv, 1) == true);
        result      = result && (BitVecGet(&bv, 2) == false);
        result      = result && (BitVecGet(&bv, 3) == true);
    
        // Try to remove first occurrence of a value that doesn't exist (after removal)
        // Check result: true, false, true, true (removed last false at index 3)
        bool result = (found == true) && (bv.length == 4);
        result      = result && (BitVecGet(&bv, 0) == true);
        result      = result && (BitVecGet(&bv, 1) == false);
        result      = result && (BitVecGet(&bv, 2) == true);
        bool result = (found == true) && (bv.length == 4);
        result      = result && (BitVecGet(&bv, 0) == true);
        result      = result && (BitVecGet(&bv, 1) == false);
        result      = result && (BitVecGet(&bv, 2) == true);
        result      = result && (BitVecGet(&bv, 3) == true);
        result      = result && (BitVecGet(&bv, 0) == true);
        result      = result && (BitVecGet(&bv, 1) == false);
        result      = result && (BitVecGet(&bv, 2) == true);
        result      = result && (BitVecGet(&bv, 3) == true);
        result      = result && (BitVecGet(&bv, 1) == false);
        result      = result && (BitVecGet(&bv, 2) == true);
        result      = result && (BitVecGet(&bv, 3) == true);
    
        // Remove last occurrence of true
        // Check result: true, false, true (removed last true at index 3)
        result = result && (found == true) && (bv.length == 3);
        result = result && (BitVecGet(&bv, 0) == true);
        result = result && (BitVecGet(&bv, 1) == false);
        result = result && (BitVecGet(&bv, 2) == true);
        result = result && (found == true) && (bv.length == 3);
        result = result && (BitVecGet(&bv, 0) == true);
        result = result && (BitVecGet(&bv, 1) == false);
        result = result && (BitVecGet(&bv, 2) == true);
        result = result && (BitVecGet(&bv, 0) == true);
        result = result && (BitVecGet(&bv, 1) == false);
        result = result && (BitVecGet(&bv, 2) == true);
    
        // Clean up
        // Check result: true, true, true (all false bits removed)
        bool result = (removed_count == 3) && (bv.length == 3);
        result      = result && (BitVecGet(&bv, 0) == true);
        result      = result && (BitVecGet(&bv, 1) == true);
        result      = result && (BitVecGet(&bv, 2) == true);
        bool result = (removed_count == 3) && (bv.length == 3);
        result      = result && (BitVecGet(&bv, 0) == true);
        result      = result && (BitVecGet(&bv, 1) == true);
        result      = result && (BitVecGet(&bv, 2) == true);
        result      = result && (BitVecGet(&bv, 0) == true);
        result      = result && (BitVecGet(&bv, 1) == true);
        result      = result && (BitVecGet(&bv, 2) == true);
    
        // Try to remove all false bits again (should return 0)
        BitVecPush(&bv, true);
        result = result && (bv.length == 1);
        result = result && (BitVecGet(&bv, 0) == true);
    
        // Clean up
        // Check that length was increased and new bits have the default value
        bool result = (bv.length == 6);
        result      = result && (BitVecGet(&bv, 0) == true);  // Original data
        result      = result && (BitVecGet(&bv, 1) == false); // Original data
        result      = result && (BitVecGet(&bv, 2) == true);  // Original data
        bool result = (bv.length == 6);
        result      = result && (BitVecGet(&bv, 0) == true);  // Original data
        result      = result && (BitVecGet(&bv, 1) == false); // Original data
        result      = result && (BitVecGet(&bv, 2) == true);  // Original data
        result      = result && (BitVecGet(&bv, 3) == false); // New data (likely default to false)
        result      = result && (BitVecGet(&bv, 0) == true);  // Original data
        result      = result && (BitVecGet(&bv, 1) == false); // Original data
        result      = result && (BitVecGet(&bv, 2) == true);  // Original data
        result      = result && (BitVecGet(&bv, 3) == false); // New data (likely default to false)
        result      = result && (BitVecGet(&bv, 4) == false); // New data (likely default to false)
        result      = result && (BitVecGet(&bv, 1) == false); // Original data
        result      = result && (BitVecGet(&bv, 2) == true);  // Original data
        result      = result && (BitVecGet(&bv, 3) == false); // New data (likely default to false)
        result      = result && (BitVecGet(&bv, 4) == false); // New data (likely default to false)
        result      = result && (BitVecGet(&bv, 5) == false); // New data (likely default to false)
        result      = result && (BitVecGet(&bv, 2) == true);  // Original data
        result      = result && (BitVecGet(&bv, 3) == false); // New data (likely default to false)
        result      = result && (BitVecGet(&bv, 4) == false); // New data (likely default to false)
        result      = result && (BitVecGet(&bv, 5) == false); // New data (likely default to false)
        result      = result && (BitVecGet(&bv, 3) == false); // New data (likely default to false)
        result      = result && (BitVecGet(&bv, 4) == false); // New data (likely default to false)
        result      = result && (BitVecGet(&bv, 5) == false); // New data (likely default to false)
    
        // Test resizing to smaller size
        // Check that length was decreased and data was truncated
        result = result && (bv.length == 2);
        result = result && (BitVecGet(&bv, 0) == true);  // Original data preserved
        result = result && (BitVecGet(&bv, 1) == false); // Original data preserved
        result = result && (bv.length == 2);
        result = result && (BitVecGet(&bv, 0) == true);  // Original data preserved
        result = result && (BitVecGet(&bv, 1) == false); // Original data preserved
    
        // Test resizing to same size (should be no-op)
        // New bits should be false
        for (u64 i = 0; i < 5; i++) {
            result = result && (BitVecGet(&bv, i) == false);
        }
        // Expected result: 1000 (1101 AND 1010)
        bool test_result = (result.length == 4);
        test_result      = test_result && (BitVecGet(&result, 0) == true);
        test_result      = test_result && (BitVecGet(&result, 1) == false);
        test_result      = test_result && (BitVecGet(&result, 2) == false);
        bool test_result = (result.length == 4);
        test_result      = test_result && (BitVecGet(&result, 0) == true);
        test_result      = test_result && (BitVecGet(&result, 1) == false);
        test_result      = test_result && (BitVecGet(&result, 2) == false);
        test_result      = test_result && (BitVecGet(&result, 3) == false);
        test_result      = test_result && (BitVecGet(&result, 0) == true);
        test_result      = test_result && (BitVecGet(&result, 1) == false);
        test_result      = test_result && (BitVecGet(&result, 2) == false);
        test_result      = test_result && (BitVecGet(&result, 3) == false);
        test_result      = test_result && (BitVecGet(&result, 1) == false);
        test_result      = test_result && (BitVecGet(&result, 2) == false);
        test_result      = test_result && (BitVecGet(&result, 3) == false);
    
        // Clean up
        // Expected result: 1110 (1100 OR 1010)
        bool test_result = (result.length == 4);
        test_result      = test_result && (BitVecGet(&result, 0) == true);
        test_result      = test_result && (BitVecGet(&result, 1) == true);
        test_result      = test_result && (BitVecGet(&result, 2) == true);
        bool test_result = (result.length == 4);
        test_result      = test_result && (BitVecGet(&result, 0) == true);
        test_result      = test_result && (BitVecGet(&result, 1) == true);
        test_result      = test_result && (BitVecGet(&result, 2) == true);
        test_result      = test_result && (BitVecGet(&result, 3) == false);
        test_result      = test_result && (BitVecGet(&result, 0) == true);
        test_result      = test_result && (BitVecGet(&result, 1) == true);
        test_result      = test_result && (BitVecGet(&result, 2) == true);
        test_result      = test_result && (BitVecGet(&result, 3) == false);
        test_result      = test_result && (BitVecGet(&result, 1) == true);
        test_result      = test_result && (BitVecGet(&result, 2) == true);
        test_result      = test_result && (BitVecGet(&result, 3) == false);
    
        // Clean up
        // Expected result: 0110 (1100 XOR 1010)
        bool test_result = (result.length == 4);
        test_result      = test_result && (BitVecGet(&result, 0) == false);
        test_result      = test_result && (BitVecGet(&result, 1) == true);
        test_result      = test_result && (BitVecGet(&result, 2) == true);
        bool test_result = (result.length == 4);
        test_result      = test_result && (BitVecGet(&result, 0) == false);
        test_result      = test_result && (BitVecGet(&result, 1) == true);
        test_result      = test_result && (BitVecGet(&result, 2) == true);
        test_result      = test_result && (BitVecGet(&result, 3) == false);
        test_result      = test_result && (BitVecGet(&result, 0) == false);
        test_result      = test_result && (BitVecGet(&result, 1) == true);
        test_result      = test_result && (BitVecGet(&result, 2) == true);
        test_result      = test_result && (BitVecGet(&result, 3) == false);
        test_result      = test_result && (BitVecGet(&result, 1) == true);
        test_result      = test_result && (BitVecGet(&result, 2) == true);
        test_result      = test_result && (BitVecGet(&result, 3) == false);
    
        // Clean up
        // Expected result: 0101 (NOT 1010)
        bool test_result = (result.length == 4);
        test_result      = test_result && (BitVecGet(&result, 0) == false);
        test_result      = test_result && (BitVecGet(&result, 1) == true);
        test_result      = test_result && (BitVecGet(&result, 2) == false);
        bool test_result = (result.length == 4);
        test_result      = test_result && (BitVecGet(&result, 0) == false);
        test_result      = test_result && (BitVecGet(&result, 1) == true);
        test_result      = test_result && (BitVecGet(&result, 2) == false);
        test_result      = test_result && (BitVecGet(&result, 3) == true);
        test_result      = test_result && (BitVecGet(&result, 0) == false);
        test_result      = test_result && (BitVecGet(&result, 1) == true);
        test_result      = test_result && (BitVecGet(&result, 2) == false);
        test_result      = test_result && (BitVecGet(&result, 3) == true);
        test_result      = test_result && (BitVecGet(&result, 1) == true);
        test_result      = test_result && (BitVecGet(&result, 2) == false);
        test_result      = test_result && (BitVecGet(&result, 3) == true);
    
        // Clean up
        // New bit[1] = 0 (filled)
        // Expected result: 0010
        test_result = test_result && (BitVecGet(&bv, 0) == false);
        test_result = test_result && (BitVecGet(&bv, 1) == false);
        test_result = test_result && (BitVecGet(&bv, 2) == true);
        // Expected result: 0010
        test_result = test_result && (BitVecGet(&bv, 0) == false);
        test_result = test_result && (BitVecGet(&bv, 1) == false);
        test_result = test_result && (BitVecGet(&bv, 2) == true);
        test_result = test_result && (BitVecGet(&bv, 3) == false);
        test_result = test_result && (BitVecGet(&bv, 0) == false);
        test_result = test_result && (BitVecGet(&bv, 1) == false);
        test_result = test_result && (BitVecGet(&bv, 2) == true);
        test_result = test_result && (BitVecGet(&bv, 3) == false);
        test_result = test_result && (BitVecGet(&bv, 1) == false);
        test_result = test_result && (BitVecGet(&bv, 2) == true);
        test_result = test_result && (BitVecGet(&bv, 3) == false);
    
        // Clean up
    
        bool test_result = (bv.length == 4);
        test_result      = test_result && (BitVecGet(&bv, 0) == true);
        test_result      = test_result && (BitVecGet(&bv, 1) == true);
        test_result      = test_result && (BitVecGet(&bv, 2) == false);
        bool test_result = (bv.length == 4);
        test_result      = test_result && (BitVecGet(&bv, 0) == true);
        test_result      = test_result && (BitVecGet(&bv, 1) == true);
        test_result      = test_result && (BitVecGet(&bv, 2) == false);
        test_result      = test_result && (BitVecGet(&bv, 3) == false);
        test_result      = test_result && (BitVecGet(&bv, 0) == true);
        test_result      = test_result && (BitVecGet(&bv, 1) == true);
        test_result      = test_result && (BitVecGet(&bv, 2) == false);
        test_result      = test_result && (BitVecGet(&bv, 3) == false);
        test_result      = test_result && (BitVecGet(&bv, 1) == true);
        test_result      = test_result && (BitVecGet(&bv, 2) == false);
        test_result      = test_result && (BitVecGet(&bv, 3) == false);
    
        // Clean up
        // Expected result: 1110 (1011 rotated left by 2)
        bool test_result = (bv.length == 4);
        test_result      = test_result && (BitVecGet(&bv, 0) == true);
        test_result      = test_result && (BitVecGet(&bv, 1) == true);
        test_result      = test_result && (BitVecGet(&bv, 2) == true);
        bool test_result = (bv.length == 4);
        test_result      = test_result && (BitVecGet(&bv, 0) == true);
        test_result      = test_result && (BitVecGet(&bv, 1) == true);
        test_result      = test_result && (BitVecGet(&bv, 2) == true);
        test_result      = test_result && (BitVecGet(&bv, 3) == false);
        test_result      = test_result && (BitVecGet(&bv, 0) == true);
        test_result      = test_result && (BitVecGet(&bv, 1) == true);
        test_result      = test_result && (BitVecGet(&bv, 2) == true);
        test_result      = test_result && (BitVecGet(&bv, 3) == false);
        test_result      = test_result && (BitVecGet(&bv, 1) == true);
        test_result      = test_result && (BitVecGet(&bv, 2) == true);
        test_result      = test_result && (BitVecGet(&bv, 3) == false);
    
        // Clean up
        // Expected result: 1101 (1011 rotated right by 1)
        bool test_result = (bv.length == 4);
        test_result      = test_result && (BitVecGet(&bv, 0) == true);
        test_result      = test_result && (BitVecGet(&bv, 1) == true);
        test_result      = test_result && (BitVecGet(&bv, 2) == false);
        bool test_result = (bv.length == 4);
        test_result      = test_result && (BitVecGet(&bv, 0) == true);
        test_result      = test_result && (BitVecGet(&bv, 1) == true);
        test_result      = test_result && (BitVecGet(&bv, 2) == false);
        test_result      = test_result && (BitVecGet(&bv, 3) == true);
        test_result      = test_result && (BitVecGet(&bv, 0) == true);
        test_result      = test_result && (BitVecGet(&bv, 1) == true);
        test_result      = test_result && (BitVecGet(&bv, 2) == false);
        test_result      = test_result && (BitVecGet(&bv, 3) == true);
        test_result      = test_result && (BitVecGet(&bv, 1) == true);
        test_result      = test_result && (BitVecGet(&bv, 2) == false);
        test_result      = test_result && (BitVecGet(&bv, 3) == true);
    
        // Clean up
        // Expected result: 1101 (1011 reversed)
        bool test_result = (bv.length == 4);
        test_result      = test_result && (BitVecGet(&bv, 0) == true);
        test_result      = test_result && (BitVecGet(&bv, 1) == true);
        test_result      = test_result && (BitVecGet(&bv, 2) == false);
        bool test_result = (bv.length == 4);
        test_result      = test_result && (BitVecGet(&bv, 0) == true);
        test_result      = test_result && (BitVecGet(&bv, 1) == true);
        test_result      = test_result && (BitVecGet(&bv, 2) == false);
        test_result      = test_result && (BitVecGet(&bv, 3) == true);
        test_result      = test_result && (BitVecGet(&bv, 0) == true);
        test_result      = test_result && (BitVecGet(&bv, 1) == true);
        test_result      = test_result && (BitVecGet(&bv, 2) == false);
        test_result      = test_result && (BitVecGet(&bv, 3) == true);
        test_result      = test_result && (BitVecGet(&bv, 1) == true);
        test_result      = test_result && (BitVecGet(&bv, 2) == false);
        test_result      = test_result && (BitVecGet(&bv, 3) == true);
    
        // Clean up
        BitVecShiftLeft(&bv, 0);
        result = result && (bv.length == 2);
        result = result && (BitVecGet(&bv, 0) == true);
    
        // Test shift larger than length (should clear all bits)
        BitVecPush(&bv, true);
        BitVecRotateRight(&bv, 0);
        result = result && (BitVecGet(&bv, 0) == true);
    
        // Test rotate by length (should be no-op)
        BitVecPush(&bv1, true);
        BitVecNot(&result_bv, &bv1);
        result = result && (BitVecGet(&result_bv, 0) == false);
    
        BitVecDeinit(&result_bv);
        BitVecReverse(&bv);
        result = result && (bv.length == 1);
        result = result && (BitVecGet(&bv, 0) == true);
    
        // Test reverse even length
        BitVecPush(&bv, false);
        BitVecReverse(&bv);
        result = result && (BitVecGet(&bv, 0) == false);
        result = result && (BitVecGet(&bv, 1) == true);
        BitVecReverse(&bv);
        result = result && (BitVecGet(&bv, 0) == false);
        result = result && (BitVecGet(&bv, 1) == true);
    
        // Test double reverse (should restore original)
        // Test double reverse (should restore original)
        BitVecReverse(&bv);
        result = result && (BitVecGet(&bv, 0) == true);
        result = result && (BitVecGet(&bv, 1) == false);
        BitVecReverse(&bv);
        result = result && (BitVecGet(&bv, 0) == true);
        result = result && (BitVecGet(&bv, 1) == false);
    
        BitVecDeinit(&bv);
        BitVecAnd(&result, &bv1, &bv2);
        test_result = test_result && (result.length == 1);
        test_result = test_result && (BitVecGet(&result, 0) == false);
    
        BitVecOr(&result, &bv1, &bv2);
    
        BitVecOr(&result, &bv1, &bv2);
        test_result = test_result && (BitVecGet(&result, 0) == true);
    
        // Test NOT on large bitvector
        // Verify NOT correctness
        for (int i = 0; i < 100; i++) {
            bool original = BitVecGet(&bv1, i);
            bool inverted = BitVecGet(&result, i);
            test_result   = test_result && (original != inverted);
        for (int i = 0; i < 100; i++) {
            bool original = BitVecGet(&bv1, i);
            bool inverted = BitVecGet(&result, i);
            test_result   = test_result && (original != inverted);
        }
        bool changed = false;
        for (int i = 0; i < (int)original.length; i++) {
            if (BitVecGet(&bv, i) != BitVecGet(&original, i)) {
                changed = true;
                break;
        BitVecShiftLeft(&bv, 2);
        result = result && (bv.length == 3);
        result = result && (BitVecGet(&bv, 0) == false); // filled with 0
        result = result && (BitVecGet(&bv, 1) == false); // filled with 0
        result = result && (BitVecGet(&bv, 2) == true);  // original bit 0
        result = result && (bv.length == 3);
        result = result && (BitVecGet(&bv, 0) == false); // filled with 0
        result = result && (BitVecGet(&bv, 1) == false); // filled with 0
        result = result && (BitVecGet(&bv, 2) == true);  // original bit 0
        result = result && (BitVecGet(&bv, 0) == false); // filled with 0
        result = result && (BitVecGet(&bv, 1) == false); // filled with 0
        result = result && (BitVecGet(&bv, 2) == true);  // original bit 0
    
        BitVecDeinit(&bv);
        bool restored = true;
        for (int i = 0; i < 8; i++) {
            if (BitVecGet(&bv, i) != BitVecGet(&original, i)) {
                restored = false;
                break;
        bool unchanged = true;
        for (int i = 0; i < 8; i++) {
            if (BitVecGet(&bv, i) != BitVecGet(&original, i)) {
                unchanged = false;
                break;
        BitVecRotateLeft(&bv, 2);
        // 10101 -> 10110 (rotated left by 2)
        result = result && (BitVecGet(&bv, 0) == true);
        result = result && (BitVecGet(&bv, 1) == false);
        result = result && (BitVecGet(&bv, 2) == true);
        // 10101 -> 10110 (rotated left by 2)
        result = result && (BitVecGet(&bv, 0) == true);
        result = result && (BitVecGet(&bv, 1) == false);
        result = result && (BitVecGet(&bv, 2) == true);
        result = result && (BitVecGet(&bv, 3) == true);
        result = result && (BitVecGet(&bv, 0) == true);
        result = result && (BitVecGet(&bv, 1) == false);
        result = result && (BitVecGet(&bv, 2) == true);
        result = result && (BitVecGet(&bv, 3) == true);
        result = result && (BitVecGet(&bv, 4) == false);
        result = result && (BitVecGet(&bv, 1) == false);
        result = result && (BitVecGet(&bv, 2) == true);
        result = result && (BitVecGet(&bv, 3) == true);
        result = result && (BitVecGet(&bv, 4) == false);
        result = result && (BitVecGet(&bv, 2) == true);
        result = result && (BitVecGet(&bv, 3) == true);
        result = result && (BitVecGet(&bv, 4) == false);
    
        BitVecDeinit(&bv);
        bool and_identity = true;
        for (int i = 0; i < 16; i++) {
            if (BitVecGet(&result, i) != BitVecGet(&bv1, i)) {
                and_identity = false;
                break;
        bool or_identity = true;
        for (int i = 0; i < 16; i++) {
            if (BitVecGet(&result, i) != BitVecGet(&bv1, i)) {
                or_identity = false;
                break;
        test_result = test_result && (result.length == 16);
        for (int i = 0; i < 16; i++) {
            test_result = test_result && (BitVecGet(&result, i) == false);
        }
        bool double_not = true;
        for (int i = 0; i < 16; i++) {
            if (BitVecGet(&result, i) != BitVecGet(&bv1, i)) {
                double_not = false;
                break;
        bool and_zero = true;
        for (int i = 0; i < 16; i++) {
            if (BitVecGet(&result, i) != false) {
                and_zero = false;
                break;
        bool or_ones = true;
        for (int i = 0; i < 16; i++) {
            if (BitVecGet(&result, i) != true) {
                or_ones = false;
                break;
        bool and_commutative = true;
        for (int i = 0; i < 12; i++) {
            if (BitVecGet(&result1, i) != BitVecGet(&result2, i)) {
                and_commutative = false;
                break;
        bool or_commutative = true;
        for (int i = 0; i < 12; i++) {
            if (BitVecGet(&result1, i) != BitVecGet(&result2, i)) {
                or_commutative = false;
                break;
        bool xor_commutative = true;
        for (int i = 0; i < 12; i++) {
            if (BitVecGet(&result1, i) != BitVecGet(&result2, i)) {
                xor_commutative = false;
                break;
        for (int i = 0; i < 1000; i += 77) { // Check every 77th bit
            bool expected = (i % 7 == 0) && (i % 11 == 0);
            bool actual   = BitVecGet(&result, i);
            test_result   = test_result && (expected == actual);
        }
        // Verify NOT correctness on sample
        for (int i = 0; i < 1000; i += 123) {
            bool original = BitVecGet(&bv1, i);
            bool inverted = BitVecGet(&result, i);
            test_result   = test_result && (original != inverted);
        for (int i = 0; i < 1000; i += 123) {
            bool original = BitVecGet(&bv1, i);
            bool inverted = BitVecGet(&result, i);
            test_result   = test_result && (original != inverted);
        }
        // First 100 bits should be 0
        for (int i = 0; i < 100; i++) {
            test_result = test_result && (BitVecGet(&result, i) == false);
        }
        // Check that data is still intact
        result = result && (bv.length == 3);
        result = result && (BitVecGet(&bv, 0) == true);
        result = result && (BitVecGet(&bv, 1) == false);
        result = result && (BitVecGet(&bv, 2) == true);
        result = result && (bv.length == 3);
        result = result && (BitVecGet(&bv, 0) == true);
        result = result && (BitVecGet(&bv, 1) == false);
        result = result && (BitVecGet(&bv, 2) == true);
        result = result && (BitVecGet(&bv, 0) == true);
        result = result && (BitVecGet(&bv, 1) == false);
        result = result && (BitVecGet(&bv, 2) == true);
    
        // Clean up
    
        // Check that data is still intact
        result = result && (BitVecGet(&bv, 0) == true);
        result = result && (BitVecGet(&bv, 1) == false);
        // Check that data is still intact
        result = result && (BitVecGet(&bv, 0) == true);
        result = result && (BitVecGet(&bv, 1) == false);
    
        // Try to set capacity smaller than current length (should not shrink below length)
    
        // Data should still be intact
        result = result && (BitVecGet(&bv, 0) == true);
        result = result && (BitVecGet(&bv, 1) == false);
        // Data should still be intact
        result = result && (BitVecGet(&bv, 0) == true);
        result = result && (BitVecGet(&bv, 1) == false);
    
        // Clean up
        // Check bv1 (should now have bv2's original content)
        result = result && (bv1.length == 2);
        result = result && (BitVecGet(&bv1, 0) == false);
        result = result && (BitVecGet(&bv1, 1) == false);
        result = result && (bv1.length == 2);
        result = result && (BitVecGet(&bv1, 0) == false);
        result = result && (BitVecGet(&bv1, 1) == false);
    
        // Check bv2 (should now have bv1's original content)
        // Check bv2 (should now have bv1's original content)
        result = result && (bv2.length == 3);
        result = result && (BitVecGet(&bv2, 0) == true);
        result = result && (BitVecGet(&bv2, 1) == false);
        result = result && (BitVecGet(&bv2, 2) == true);
        result = result && (bv2.length == 3);
        result = result && (BitVecGet(&bv2, 0) == true);
        result = result && (BitVecGet(&bv2, 1) == false);
        result = result && (BitVecGet(&bv2, 2) == true);
        result = result && (BitVecGet(&bv2, 0) == true);
        result = result && (BitVecGet(&bv2, 1) == false);
        result = result && (BitVecGet(&bv2, 2) == true);
    
        // Clean up
    
        for (u64 i = 0; i < original.length; i++) {
            result = result && (BitVecGet(&clone, i) == BitVecGet(&original, i));
        }
        // Clone should remain unchanged
        result = result && (clone.length == 4) && (original.length == 5);
        result = result && (BitVecGet(&clone, 0) == true);
        result = result && (BitVecGet(&clone, 1) == false);
        result = result && (BitVecGet(&clone, 2) == true);
        result = result && (clone.length == 4) && (original.length == 5);
        result = result && (BitVecGet(&clone, 0) == true);
        result = result && (BitVecGet(&clone, 1) == false);
        result = result && (BitVecGet(&clone, 2) == true);
        result = result && (BitVecGet(&clone, 3) == false);
        result = result && (BitVecGet(&clone, 0) == true);
        result = result && (BitVecGet(&clone, 1) == false);
        result = result && (BitVecGet(&clone, 2) == true);
        result = result && (BitVecGet(&clone, 3) == false);
        result = result && (BitVecGet(&clone, 1) == false);
        result = result && (BitVecGet(&clone, 2) == true);
        result = result && (BitVecGet(&clone, 3) == false);
    
        // Modify clone to verify independence
    
        // Original should remain unchanged at index 0
        result = result && (BitVecGet(&original, 0) == true);
        result = result && (BitVecGet(&clone, 0) == false);
        // Original should remain unchanged at index 0
        result = result && (BitVecGet(&original, 0) == true);
        result = result && (BitVecGet(&clone, 0) == false);
    
        // Clean up
        BitVecShrinkToFit(&bv);
        result = result && (bv.length == 1) && (bv.capacity >= 1);
        result = result && (BitVecGet(&bv, 0) == true);
    
        // Test multiple shrinks (should be safe)
        result = result && (bv.length == original_length);
        for (u64 i = 0; i < bv.length; i++) {
            result = result && (BitVecGet(&bv, i) == (i % 2 == 0));
        }
        result = result && (bv1.length == 0);
        result = result && (bv2.length == 2);
        result = result && (BitVecGet(&bv2, 0) == true);
        result = result && (BitVecGet(&bv2, 1) == false);
        result = result && (bv2.length == 2);
        result = result && (BitVecGet(&bv2, 0) == true);
        result = result && (BitVecGet(&bv2, 1) == false);
    
        // Test swap with large data
        BitVecSwap(&bv1, &bv2);
        result = result && (bv1.length == 2) && (bv2.length == 1000);
        result = result && (BitVecGet(&bv2, 0) == true); // 0 % 3 == 0
        result = result && (BitVecGet(&bv2, 999) == (999 % 3 == 0));
        result = result && (bv1.length == 2) && (bv2.length == 1000);
        result = result && (BitVecGet(&bv2, 0) == true); // 0 % 3 == 0
        result = result && (BitVecGet(&bv2, 999) == (999 % 3 == 0));
    
        // Test swapping with itself (should be safe)
        BitVec clone2 = BitVecClone(&bv);
        result        = result && (clone2.length == 1);
        result        = result && (BitVecGet(&clone2, 0) == true);
        BitVecDeinit(&clone2);
        // Verify all bits match
        for (u64 i = 0; i < 1000; i++) {
            result = result && (BitVecGet(&clone3, i) == BitVecGet(&bv, i));
        }
    
        // Test independence - modify original
        BitVecSet(&bv, 0, !BitVecGet(&bv, 0));
        result = result && (BitVecGet(&clone3, 0) != BitVecGet(&bv, 0));
        // Test independence - modify original
        BitVecSet(&bv, 0, !BitVecGet(&bv, 0));
        result = result && (BitVecGet(&clone3, 0) != BitVecGet(&bv, 0));
    
        BitVecDeinit(&clone3);
            result = result && (clone.length == cycle * 10);
            if (cycle > 0) {
                result = result && (BitVecGet(&clone, 0) == true); // 0 % 2 == 0
            }
        // Check result
        bool result = (bv.length == 4);
        result      = result && (BitVecGet(&bv, 0) == true);
        result      = result && (BitVecGet(&bv, 1) == false);
        result      = result && (BitVecGet(&bv, 2) == true);
        bool result = (bv.length == 4);
        result      = result && (BitVecGet(&bv, 0) == true);
        result      = result && (BitVecGet(&bv, 1) == false);
        result      = result && (BitVecGet(&bv, 2) == true);
        result      = result && (BitVecGet(&bv, 3) == true);
        result      = result && (BitVecGet(&bv, 0) == true);
        result      = result && (BitVecGet(&bv, 1) == false);
        result      = result && (BitVecGet(&bv, 2) == true);
        result      = result && (BitVecGet(&bv, 3) == true);
        result      = result && (BitVecGet(&bv, 1) == false);
        result      = result && (BitVecGet(&bv, 2) == true);
        result      = result && (BitVecGet(&bv, 3) == true);
    
        // Test with empty string
    
        for (u64 i = 0; i < bv.length; i++) {
            if (BitVecGet(&bv, i)) {
                true_count++;
            } else {
    
        for (u64 i = 0; i < bv.length; i++) {
            if (BitVecGet(&bv, i)) {
                true_count++;
            } else {
        bool all_false = true;
        for (u64 i = 0; i < zero_bv.length; i++) {
            if (BitVecGet(&zero_bv, i)) {
                all_false = false;
                break;
        BitVec bv2 = BitVecFromStr("1");
        result     = result && (bv2.length == 1);
        result     = result && (BitVecGet(&bv2, 0) == true);
        BitVecDeinit(&bv2);
        BitVec bv3 = BitVecFromStr(long_str);
        result     = result && (bv3.length == 1000);
        result     = result && (BitVecGet(&bv3, 0) == true);
        result     = result && (BitVecGet(&bv3, 1) == false);
        BitVecDeinit(&bv3);
        result     = result && (bv3.length == 1000);
        result     = result && (BitVecGet(&bv3, 0) == true);
        result     = result && (BitVecGet(&bv3, 1) == false);
        BitVecDeinit(&bv3);
        for (u64 i = 0; i < recovered_bv.length; i++) {
            bool expected = (i % 3) == 0;
            bool actual   = BitVecGet(&recovered_bv, i);
            if (expected != actual) {
                recovered_pattern_correct = false;
        for (u64 i = 0; i < large_from_str.length; i++) {
            bool expected = (i % 7) == 0;
            bool actual   = BitVecGet(&large_from_str, i);
            if (expected != actual) {
                large_pattern_correct = false;
        // Expected result: 1000 (1101 AND 1010)
        bool test_result = (result.length == 4);
        test_result      = test_result && (BitVecGet(&result, 0) == true);
        test_result      = test_result && (BitVecGet(&result, 1) == false);
        test_result      = test_result && (BitVecGet(&result, 2) == false);
        bool test_result = (result.length == 4);
        test_result      = test_result && (BitVecGet(&result, 0) == true);
        test_result      = test_result && (BitVecGet(&result, 1) == false);
        test_result      = test_result && (BitVecGet(&result, 2) == false);
        test_result      = test_result && (BitVecGet(&result, 3) == false);
        test_result      = test_result && (BitVecGet(&result, 0) == true);
        test_result      = test_result && (BitVecGet(&result, 1) == false);
        test_result      = test_result && (BitVecGet(&result, 2) == false);
        test_result      = test_result && (BitVecGet(&result, 3) == false);
        test_result      = test_result && (BitVecGet(&result, 1) == false);
        test_result      = test_result && (BitVecGet(&result, 2) == false);
        test_result      = test_result && (BitVecGet(&result, 3) == false);
    
        // Clean up
        // Expected result: 1110 (1100 OR 1010)
        bool test_result = (result.length == 4);
        test_result      = test_result && (BitVecGet(&result, 0) == true);
        test_result      = test_result && (BitVecGet(&result, 1) == true);
        test_result      = test_result && (BitVecGet(&result, 2) == true);
        bool test_result = (result.length == 4);
        test_result      = test_result && (BitVecGet(&result, 0) == true);
        test_result      = test_result && (BitVecGet(&result, 1) == true);
        test_result      = test_result && (BitVecGet(&result, 2) == true);
        test_result      = test_result && (BitVecGet(&result, 3) == false);
        test_result      = test_result && (BitVecGet(&result, 0) == true);
        test_result      = test_result && (BitVecGet(&result, 1) == true);
        test_result      = test_result && (BitVecGet(&result, 2) == true);
        test_result      = test_result && (BitVecGet(&result, 3) == false);
        test_result      = test_result && (BitVecGet(&result, 1) == true);
        test_result      = test_result && (BitVecGet(&result, 2) == true);
        test_result      = test_result && (BitVecGet(&result, 3) == false);
    
        // Clean up
        // Expected result: 0110 (1100 XOR 1010)
        bool test_result = (result.length == 4);
        test_result      = test_result && (BitVecGet(&result, 0) == false);
        test_result      = test_result && (BitVecGet(&result, 1) == true);
        test_result      = test_result && (BitVecGet(&result, 2) == true);
        bool test_result = (result.length == 4);
        test_result      = test_result && (BitVecGet(&result, 0) == false);
        test_result      = test_result && (BitVecGet(&result, 1) == true);
        test_result      = test_result && (BitVecGet(&result, 2) == true);
        test_result      = test_result && (BitVecGet(&result, 3) == false);
        test_result      = test_result && (BitVecGet(&result, 0) == false);
        test_result      = test_result && (BitVecGet(&result, 1) == true);
        test_result      = test_result && (BitVecGet(&result, 2) == true);
        test_result      = test_result && (BitVecGet(&result, 3) == false);
        test_result      = test_result && (BitVecGet(&result, 1) == true);
        test_result      = test_result && (BitVecGet(&result, 2) == true);
        test_result      = test_result && (BitVecGet(&result, 3) == false);
    
        // Clean up
        // Expected result: 0101 (NOT 1010)
        bool test_result = (result.length == 4);
        test_result      = test_result && (BitVecGet(&result, 0) == false);
        test_result      = test_result && (BitVecGet(&result, 1) == true);
        test_result      = test_result && (BitVecGet(&result, 2) == false);
        bool test_result = (result.length == 4);
        test_result      = test_result && (BitVecGet(&result, 0) == false);
        test_result      = test_result && (BitVecGet(&result, 1) == true);
        test_result      = test_result && (BitVecGet(&result, 2) == false);
        test_result      = test_result && (BitVecGet(&result, 3) == true);
        test_result      = test_result && (BitVecGet(&result, 0) == false);
        test_result      = test_result && (BitVecGet(&result, 1) == true);
        test_result      = test_result && (BitVecGet(&result, 2) == false);
        test_result      = test_result && (BitVecGet(&result, 3) == true);
        test_result      = test_result && (BitVecGet(&result, 1) == true);
        test_result      = test_result && (BitVecGet(&result, 2) == false);
        test_result      = test_result && (BitVecGet(&result, 3) == true);
    
        // Clean up
        // New bit[1] = 0 (filled)
        // Expected result: 0010
        test_result = test_result && (BitVecGet(&bv, 0) == false);
        test_result = test_result && (BitVecGet(&bv, 1) == false);
        test_result = test_result && (BitVecGet(&bv, 2) == true);
        // Expected result: 0010
        test_result = test_result && (BitVecGet(&bv, 0) == false);
        test_result = test_result && (BitVecGet(&bv, 1) == false);
        test_result = test_result && (BitVecGet(&bv, 2) == true);
        test_result = test_result && (BitVecGet(&bv, 3) == false);
        test_result = test_result && (BitVecGet(&bv, 0) == false);
        test_result = test_result && (BitVecGet(&bv, 1) == false);
        test_result = test_result && (BitVecGet(&bv, 2) == true);
        test_result = test_result && (BitVecGet(&bv, 3) == false);
        test_result = test_result && (BitVecGet(&bv, 1) == false);
        test_result = test_result && (BitVecGet(&bv, 2) == true);
        test_result = test_result && (BitVecGet(&bv, 3) == false);
    
        // Clean up
    
        bool test_result = (bv.length == 4);
        test_result      = test_result && (BitVecGet(&bv, 0) == true);
        test_result      = test_result && (BitVecGet(&bv, 1) == true);
        test_result      = test_result && (BitVecGet(&bv, 2) == false);
        bool test_result = (bv.length == 4);
        test_result      = test_result && (BitVecGet(&bv, 0) == true);
        test_result      = test_result && (BitVecGet(&bv, 1) == true);
        test_result      = test_result && (BitVecGet(&bv, 2) == false);
        test_result      = test_result && (BitVecGet(&bv, 3) == false);
        test_result      = test_result && (BitVecGet(&bv, 0) == true);
        test_result      = test_result && (BitVecGet(&bv, 1) == true);
        test_result      = test_result && (BitVecGet(&bv, 2) == false);
        test_result      = test_result && (BitVecGet(&bv, 3) == false);
        test_result      = test_result && (BitVecGet(&bv, 1) == true);
        test_result      = test_result && (BitVecGet(&bv, 2) == false);
        test_result      = test_result && (BitVecGet(&bv, 3) == false);
    
        // Clean up
        // Expected result: 1110 (1011 rotated left by 2)
        bool test_result = (bv.length == 4);
        test_result      = test_result && (BitVecGet(&bv, 0) == true);
        test_result      = test_result && (BitVecGet(&bv, 1) == true);
        test_result      = test_result && (BitVecGet(&bv, 2) == true);
        bool test_result = (bv.length == 4);
        test_result      = test_result && (BitVecGet(&bv, 0) == true);
        test_result      = test_result && (BitVecGet(&bv, 1) == true);
        test_result      = test_result && (BitVecGet(&bv, 2) == true);
        test_result      = test_result && (BitVecGet(&bv, 3) == false);
        test_result      = test_result && (BitVecGet(&bv, 0) == true);
        test_result      = test_result && (BitVecGet(&bv, 1) == true);
        test_result      = test_result && (BitVecGet(&bv, 2) == true);
        test_result      = test_result && (BitVecGet(&bv, 3) == false);
        test_result      = test_result && (BitVecGet(&bv, 1) == true);
        test_result      = test_result && (BitVecGet(&bv, 2) == true);
        test_result      = test_result && (BitVecGet(&bv, 3) == false);
    
        // Clean up
        // Expected result: 1101 (1011 rotated right by 1)
        bool test_result = (bv.length == 4);
        test_result      = test_result && (BitVecGet(&bv, 0) == true);
        test_result      = test_result && (BitVecGet(&bv, 1) == true);
        test_result      = test_result && (BitVecGet(&bv, 2) == false);
        bool test_result = (bv.length == 4);
        test_result      = test_result && (BitVecGet(&bv, 0) == true);
        test_result      = test_result && (BitVecGet(&bv, 1) == true);
        test_result      = test_result && (BitVecGet(&bv, 2) == false);
        test_result      = test_result && (BitVecGet(&bv, 3) == true);
        test_result      = test_result && (BitVecGet(&bv, 0) == true);
        test_result      = test_result && (BitVecGet(&bv, 1) == true);
        test_result      = test_result && (BitVecGet(&bv, 2) == false);
        test_result      = test_result && (BitVecGet(&bv, 3) == true);
        test_result      = test_result && (BitVecGet(&bv, 1) == true);
        test_result      = test_result && (BitVecGet(&bv, 2) == false);
        test_result      = test_result && (BitVecGet(&bv, 3) == true);
    
        // Clean up
        // Expected result: 1101 (1011 reversed)
        bool test_result = (bv.length == 4);
        test_result      = test_result && (BitVecGet(&bv, 0) == true);
        test_result      = test_result && (BitVecGet(&bv, 1) == true);
        test_result      = test_result && (BitVecGet(&bv, 2) == false);
        bool test_result = (bv.length == 4);
        test_result      = test_result && (BitVecGet(&bv, 0) == true);
        test_result      = test_result && (BitVecGet(&bv, 1) == true);
        test_result      = test_result && (BitVecGet(&bv, 2) == false);
        test_result      = test_result && (BitVecGet(&bv, 3) == true);
        test_result      = test_result && (BitVecGet(&bv, 0) == true);
        test_result      = test_result && (BitVecGet(&bv, 1) == true);
        test_result      = test_result && (BitVecGet(&bv, 2) == false);
        test_result      = test_result && (BitVecGet(&bv, 3) == true);
        test_result      = test_result && (BitVecGet(&bv, 1) == true);
        test_result      = test_result && (BitVecGet(&bv, 2) == false);
        test_result      = test_result && (BitVecGet(&bv, 3) == true);
    
        // Clean up
        BitVecShiftLeft(&bv, 0);
        result = result && (bv.length == 2);
        result = result && (BitVecGet(&bv, 0) == true);
    
        // Test shift larger than length (should clear all bits)
        BitVecPush(&bv, true);
        BitVecRotateRight(&bv, 0);
        result = result && (BitVecGet(&bv, 0) == true);
    
        // Test rotate by length (should be no-op)
        BitVecPush(&bv1, true);
        BitVecNot(&result_bv, &bv1);
        result = result && (BitVecGet(&result_bv, 0) == false);
    
        BitVecDeinit(&result_bv);
        BitVecReverse(&bv);
        result = result && (bv.length == 1);
        result = result && (BitVecGet(&bv, 0) == true);
    
        // Test reverse even length
        BitVecPush(&bv, false);
        BitVecReverse(&bv);
        result = result && (BitVecGet(&bv, 0) == false);
        result = result && (BitVecGet(&bv, 1) == true);
        BitVecReverse(&bv);
        result = result && (BitVecGet(&bv, 0) == false);
        result = result && (BitVecGet(&bv, 1) == true);
    
        // Test double reverse (should restore original)
        // Test double reverse (should restore original)
        BitVecReverse(&bv);
        result = result && (BitVecGet(&bv, 0) == true);
        result = result && (BitVecGet(&bv, 1) == false);
        BitVecReverse(&bv);
        result = result && (BitVecGet(&bv, 0) == true);
        result = result && (BitVecGet(&bv, 1) == false);
    
        BitVecDeinit(&bv);
        BitVecAnd(&result, &bv1, &bv2);
        test_result = test_result && (result.length == 1);
        test_result = test_result && (BitVecGet(&result, 0) == false);
    
        BitVecOr(&result, &bv1, &bv2);
    
        BitVecOr(&result, &bv1, &bv2);
        test_result = test_result && (BitVecGet(&result, 0) == true);
    
        // Test NOT on large bitvector
        // Verify NOT correctness
        for (int i = 0; i < 100; i++) {
            bool original = BitVecGet(&bv1, i);
            bool inverted = BitVecGet(&result, i);
            test_result   = test_result && (original != inverted);
        for (int i = 0; i < 100; i++) {
            bool original = BitVecGet(&bv1, i);
            bool inverted = BitVecGet(&result, i);
            test_result   = test_result && (original != inverted);
        }
        bool changed = false;
        for (int i = 0; i < (int)original.length; i++) {
            if (BitVecGet(&bv, i) != BitVecGet(&original, i)) {
                changed = true;
                break;
        BitVecShiftLeft(&bv, 2);
        result = result && (bv.length == 3);
        result = result && (BitVecGet(&bv, 0) == false); // filled with 0
        result = result && (BitVecGet(&bv, 1) == false); // filled with 0
        result = result && (BitVecGet(&bv, 2) == true);  // original bit 0
        result = result && (bv.length == 3);
        result = result && (BitVecGet(&bv, 0) == false); // filled with 0
        result = result && (BitVecGet(&bv, 1) == false); // filled with 0
        result = result && (BitVecGet(&bv, 2) == true);  // original bit 0
        result = result && (BitVecGet(&bv, 0) == false); // filled with 0
        result = result && (BitVecGet(&bv, 1) == false); // filled with 0
        result = result && (BitVecGet(&bv, 2) == true);  // original bit 0
    
        BitVecDeinit(&bv);
        bool restored = true;
        for (int i = 0; i < 8; i++) {
            if (BitVecGet(&bv, i) != BitVecGet(&original, i)) {
                restored = false;
                break;
        bool unchanged = true;
        for (int i = 0; i < 8; i++) {
            if (BitVecGet(&bv, i) != BitVecGet(&original, i)) {
                unchanged = false;
                break;
        BitVecRotateLeft(&bv, 2);
        // 10101 -> 10110 (rotated left by 2)
        result = result && (BitVecGet(&bv, 0) == true);
        result = result && (BitVecGet(&bv, 1) == false);
        result = result && (BitVecGet(&bv, 2) == true);
        // 10101 -> 10110 (rotated left by 2)
        result = result && (BitVecGet(&bv, 0) == true);
        result = result && (BitVecGet(&bv, 1) == false);
        result = result && (BitVecGet(&bv, 2) == true);
        result = result && (BitVecGet(&bv, 3) == true);
        result = result && (BitVecGet(&bv, 0) == true);
        result = result && (BitVecGet(&bv, 1) == false);
        result = result && (BitVecGet(&bv, 2) == true);
        result = result && (BitVecGet(&bv, 3) == true);
        result = result && (BitVecGet(&bv, 4) == false);
        result = result && (BitVecGet(&bv, 1) == false);
        result = result && (BitVecGet(&bv, 2) == true);
        result = result && (BitVecGet(&bv, 3) == true);
        result = result && (BitVecGet(&bv, 4) == false);
        result = result && (BitVecGet(&bv, 2) == true);
        result = result && (BitVecGet(&bv, 3) == true);
        result = result && (BitVecGet(&bv, 4) == false);
    
        BitVecDeinit(&bv);
        bool and_identity = true;
        for (int i = 0; i < 16; i++) {
            if (BitVecGet(&result, i) != BitVecGet(&bv1, i)) {
                and_identity = false;
                break;
        bool or_identity = true;
        for (int i = 0; i < 16; i++) {
            if (BitVecGet(&result, i) != BitVecGet(&bv1, i)) {
                or_identity = false;
                break;
        test_result = test_result && (result.length == 16);
        for (int i = 0; i < 16; i++) {
            test_result = test_result && (BitVecGet(&result, i) == false);
        }
        bool double_not = true;
        for (int i = 0; i < 16; i++) {
            if (BitVecGet(&result, i) != BitVecGet(&bv1, i)) {
                double_not = false;
                break;
        bool and_zero = true;
        for (int i = 0; i < 16; i++) {
            if (BitVecGet(&result, i) != false) {
                and_zero = false;
                break;
        bool or_ones = true;
        for (int i = 0; i < 16; i++) {
            if (BitVecGet(&result, i) != true) {
                or_ones = false;
                break;
        bool and_commutative = true;
        for (int i = 0; i < 12; i++) {
            if (BitVecGet(&result1, i) != BitVecGet(&result2, i)) {
                and_commutative = false;
                break;
        bool or_commutative = true;
        for (int i = 0; i < 12; i++) {
            if (BitVecGet(&result1, i) != BitVecGet(&result2, i)) {
                or_commutative = false;
                break;
        bool xor_commutative = true;
        for (int i = 0; i < 12; i++) {
            if (BitVecGet(&result1, i) != BitVecGet(&result2, i)) {
                xor_commutative = false;
                break;
        for (int i = 0; i < 1000; i += 77) { // Check every 77th bit
            bool expected = (i % 7 == 0) && (i % 11 == 0);
            bool actual   = BitVecGet(&result, i);
            test_result   = test_result && (expected == actual);
        }
        // Verify NOT correctness on sample
        for (int i = 0; i < 1000; i += 123) {
            bool original = BitVecGet(&bv1, i);
            bool inverted = BitVecGet(&result, i);
            test_result   = test_result && (original != inverted);
        for (int i = 0; i < 1000; i += 123) {
            bool original = BitVecGet(&bv1, i);
            bool inverted = BitVecGet(&result, i);
            test_result   = test_result && (original != inverted);
        }
        // First 100 bits should be 0
        for (int i = 0; i < 100; i++) {
            test_result = test_result && (BitVecGet(&result, i) == false);
        }
    
        // Check each bit
        result = result && (BitVecGet(&bv, 0) == true);
        result = result && (BitVecGet(&bv, 1) == false);
        result = result && (BitVecGet(&bv, 2) == true);
        // Check each bit
        result = result && (BitVecGet(&bv, 0) == true);
        result = result && (BitVecGet(&bv, 1) == false);
        result = result && (BitVecGet(&bv, 2) == true);
        result = result && (BitVecGet(&bv, 3) == true);
        result = result && (BitVecGet(&bv, 0) == true);
        result = result && (BitVecGet(&bv, 1) == false);
        result = result && (BitVecGet(&bv, 2) == true);
        result = result && (BitVecGet(&bv, 3) == true);
        result = result && (BitVecGet(&bv, 4) == false);
        result = result && (BitVecGet(&bv, 1) == false);
        result = result && (BitVecGet(&bv, 2) == true);
        result = result && (BitVecGet(&bv, 3) == true);
        result = result && (BitVecGet(&bv, 4) == false);
        result = result && (BitVecGet(&bv, 2) == true);
        result = result && (BitVecGet(&bv, 3) == true);
        result = result && (BitVecGet(&bv, 4) == false);
    
        // Clean up
    
        // Check first bit
        bool result = (bv.length == 1 && BitVecGet(&bv, 0) == true);
    
        // Insert at the end
        // Check bits
        result = result && (bv.length == 2);
        result = result && (BitVecGet(&bv, 0) == true);
        result = result && (BitVecGet(&bv, 1) == false);
        result = result && (bv.length == 2);
        result = result && (BitVecGet(&bv, 0) == true);
        result = result && (BitVecGet(&bv, 1) == false);
    
        // Insert in the middle
        // Check all bits
        result = result && (bv.length == 3);
        result = result && (BitVecGet(&bv, 0) == true);
        result = result && (BitVecGet(&bv, 1) == true);
        result = result && (BitVecGet(&bv, 2) == false);
        result = result && (bv.length == 3);
        result = result && (BitVecGet(&bv, 0) == true);
        result = result && (BitVecGet(&bv, 1) == true);
        result = result && (BitVecGet(&bv, 2) == false);
        result = result && (BitVecGet(&bv, 0) == true);
        result = result && (BitVecGet(&bv, 1) == true);
        result = result && (BitVecGet(&bv, 2) == false);
    
        // Clean up
        // Check result: false, true, true, true, false
        bool result = (bv.length == 5);
        result      = result && (BitVecGet(&bv, 0) == false);
        result      = result && (BitVecGet(&bv, 1) == true);
        result      = result && (BitVecGet(&bv, 2) == true);
        bool result = (bv.length == 5);
        result      = result && (BitVecGet(&bv, 0) == false);
        result      = result && (BitVecGet(&bv, 1) == true);
        result      = result && (BitVecGet(&bv, 2) == true);
        result      = result && (BitVecGet(&bv, 3) == true);
        result      = result && (BitVecGet(&bv, 0) == false);
        result      = result && (BitVecGet(&bv, 1) == true);
        result      = result && (BitVecGet(&bv, 2) == true);
        result      = result && (BitVecGet(&bv, 3) == true);
        result      = result && (BitVecGet(&bv, 4) == false);
        result      = result && (BitVecGet(&bv, 1) == true);
        result      = result && (BitVecGet(&bv, 2) == true);
        result      = result && (BitVecGet(&bv, 3) == true);
        result      = result && (BitVecGet(&bv, 4) == false);
        result      = result && (BitVecGet(&bv, 2) == true);
        result      = result && (BitVecGet(&bv, 3) == true);
        result      = result && (BitVecGet(&bv, 4) == false);
    
        // Clean up
        // Check result: true, true, true, true, false
        bool result = (bv.length == 5);
        result      = result && (BitVecGet(&bv, 0) == true);
        result      = result && (BitVecGet(&bv, 1) == true);
        result      = result && (BitVecGet(&bv, 2) == true);
        bool result = (bv.length == 5);
        result      = result && (BitVecGet(&bv, 0) == true);
        result      = result && (BitVecGet(&bv, 1) == true);
        result      = result && (BitVecGet(&bv, 2) == true);
        result      = result && (BitVecGet(&bv, 3) == true);
        result      = result && (BitVecGet(&bv, 0) == true);
        result      = result && (BitVecGet(&bv, 1) == true);
        result      = result && (BitVecGet(&bv, 2) == true);
        result      = result && (BitVecGet(&bv, 3) == true);
        result      = result && (BitVecGet(&bv, 4) == false);
        result      = result && (BitVecGet(&bv, 1) == true);
        result      = result && (BitVecGet(&bv, 2) == true);
        result      = result && (BitVecGet(&bv, 3) == true);
        result      = result && (BitVecGet(&bv, 4) == false);
        result      = result && (BitVecGet(&bv, 2) == true);
        result      = result && (BitVecGet(&bv, 3) == true);
        result      = result && (BitVecGet(&bv, 4) == false);
    
        // Clean up
        // Pattern 1011 gets inserted as individual bits
        bool result = (bv.length == 6);
        result      = result && (BitVecGet(&bv, 0) == false); // original
        result      = result && (BitVecGet(&bv, 1) == true);  // bit 0 of pattern (LSB)
        result      = result && (BitVecGet(&bv, 2) == true);  // bit 1 of pattern
        bool result = (bv.length == 6);
        result      = result && (BitVecGet(&bv, 0) == false); // original
        result      = result && (BitVecGet(&bv, 1) == true);  // bit 0 of pattern (LSB)
        result      = result && (BitVecGet(&bv, 2) == true);  // bit 1 of pattern
        result      = result && (BitVecGet(&bv, 3) == false); // bit 2 of pattern
        result      = result && (BitVecGet(&bv, 0) == false); // original
        result      = result && (BitVecGet(&bv, 1) == true);  // bit 0 of pattern (LSB)
        result      = result && (BitVecGet(&bv, 2) == true);  // bit 1 of pattern
        result      = result && (BitVecGet(&bv, 3) == false); // bit 2 of pattern
        result      = result && (BitVecGet(&bv, 4) == true);  // bit 3 of pattern (MSB)
        result      = result && (BitVecGet(&bv, 1) == true);  // bit 0 of pattern (LSB)
        result      = result && (BitVecGet(&bv, 2) == true);  // bit 1 of pattern
        result      = result && (BitVecGet(&bv, 3) == false); // bit 2 of pattern
        result      = result && (BitVecGet(&bv, 4) == true);  // bit 3 of pattern (MSB)
        result      = result && (BitVecGet(&bv, 5) == false); // original
        result      = result && (BitVecGet(&bv, 2) == true);  // bit 1 of pattern
        result      = result && (BitVecGet(&bv, 3) == false); // bit 2 of pattern
        result      = result && (BitVecGet(&bv, 4) == true);  // bit 3 of pattern (MSB)
        result      = result && (BitVecGet(&bv, 5) == false); // original
        result      = result && (BitVecGet(&bv, 3) == false); // bit 2 of pattern
        result      = result && (BitVecGet(&bv, 4) == true);  // bit 3 of pattern (MSB)
        result      = result && (BitVecGet(&bv, 5) == false); // original
    
        // Test with different pattern - 0x05 (0101 in binary) using only 3 bits
        // Check result: true, false, true, true (3 bits: 101)
        result = result && (bv2.length == 4);
        result = result && (BitVecGet(&bv2, 0) == true);  // bit 0 of pattern (LSB)
        result = result && (BitVecGet(&bv2, 1) == false); // bit 1 of pattern
        result = result && (BitVecGet(&bv2, 2) == true);  // bit 2 of pattern
        result = result && (bv2.length == 4);
        result = result && (BitVecGet(&bv2, 0) == true);  // bit 0 of pattern (LSB)
        result = result && (BitVecGet(&bv2, 1) == false); // bit 1 of pattern
        result = result && (BitVecGet(&bv2, 2) == true);  // bit 2 of pattern
        result = result && (BitVecGet(&bv2, 3) == true);  // original
        result = result && (BitVecGet(&bv2, 0) == true);  // bit 0 of pattern (LSB)
        result = result && (BitVecGet(&bv2, 1) == false); // bit 1 of pattern
        result = result && (BitVecGet(&bv2, 2) == true);  // bit 2 of pattern
        result = result && (BitVecGet(&bv2, 3) == true);  // original
        result = result && (BitVecGet(&bv2, 1) == false); // bit 1 of pattern
        result = result && (BitVecGet(&bv2, 2) == true);  // bit 2 of pattern
        result = result && (BitVecGet(&bv2, 3) == true);  // original
    
        // Clean up
        BitVecInsertRange(&bv, 1, 2, false);
        result = result && (bv.length == 3);
        result = result && (BitVecGet(&bv, 1) == false);
        result = result && (BitVecGet(&bv, 2) == false);
        result = result && (bv.length == 3);
        result = result && (BitVecGet(&bv, 1) == false);
        result = result && (BitVecGet(&bv, 2) == false);
    
        // Test large range insertion
        BitVecInsertRange(&bv, 0, 1000, true);
        result = result && (bv.length == 1000);
        result = result && (BitVecGet(&bv, 0) == true);
        result = result && (BitVecGet(&bv, 999) == true);
        result = result && (bv.length == 1000);
        result = result && (BitVecGet(&bv, 0) == true);
        result = result && (BitVecGet(&bv, 999) == true);
    
        BitVecDeinit(&bv);
        BitVecPush(&source, true);
        BitVecInsertMultiple(&bv, 0, &source);
        result = result && (bv.length == 1) && (BitVecGet(&bv, 0) == true);
    
        // Test inserting large bitvec
        BitVecInsertMultiple(&bv, 1, &source);
        result = result && (bv.length == 501);
        result = result && (BitVecGet(&bv, 1) == false);
        result = result && (BitVecGet(&bv, 500) == false);
        result = result && (bv.length == 501);
        result = result && (BitVecGet(&bv, 1) == false);
        result = result && (BitVecGet(&bv, 500) == false);
    
        BitVecDeinit(&bv);
        BitVecInsertPattern(&bv, 0, 0xAA, 8);            // 10101010 pattern
        result = result && (bv.length == 8);
        result = result && (BitVecGet(&bv, 0) == false); // First bit of 0xAA
        result = result && (BitVecGet(&bv, 1) == true);  // Second bit
        result = result && (bv.length == 8);
        result = result && (BitVecGet(&bv, 0) == false); // First bit of 0xAA
        result = result && (BitVecGet(&bv, 1) == true);  // Second bit
    
        BitVecDeinit(&bv);
    // Test BitVecGet function
    bool test_bitvec_get(void) {
        WriteFmt("Testing BitVecGet\n");
    
        BitVec bv = BitVecInit();
    
        // Test getting each bit
        bool result = (BitVecGet(&bv, 0) == true);
        result      = result && (BitVecGet(&bv, 1) == false);
        result      = result && (BitVecGet(&bv, 2) == true);
        // Test getting each bit
        bool result = (BitVecGet(&bv, 0) == true);
        result      = result && (BitVecGet(&bv, 1) == false);
        result      = result && (BitVecGet(&bv, 2) == true);
        result      = result && (BitVecGet(&bv, 3) == false);
        bool result = (BitVecGet(&bv, 0) == true);
        result      = result && (BitVecGet(&bv, 1) == false);
        result      = result && (BitVecGet(&bv, 2) == true);
        result      = result && (BitVecGet(&bv, 3) == false);
        result      = result && (BitVecGet(&bv, 1) == false);
        result      = result && (BitVecGet(&bv, 2) == true);
        result      = result && (BitVecGet(&bv, 3) == false);
    
        // Clean up
    
        // Verify changes
        bool result = (BitVecGet(&bv, 0) == true);
        result      = result && (BitVecGet(&bv, 1) == false);
        result      = result && (BitVecGet(&bv, 2) == true);
        // Verify changes
        bool result = (BitVecGet(&bv, 0) == true);
        result      = result && (BitVecGet(&bv, 1) == false);
        result      = result && (BitVecGet(&bv, 2) == true);
        bool result = (BitVecGet(&bv, 0) == true);
        result      = result && (BitVecGet(&bv, 1) == false);
        result      = result && (BitVecGet(&bv, 2) == true);
    
        // Test setting back to false
        // Test setting back to false
        BitVecSet(&bv, 0, false);
        result = result && (BitVecGet(&bv, 0) == false);
    
        // Clean up
    
        // Verify flipped values: should now be 010
        bool result = (BitVecGet(&bv, 0) == false);
        result      = result && (BitVecGet(&bv, 1) == true);
        result      = result && (BitVecGet(&bv, 2) == false);
        // Verify flipped values: should now be 010
        bool result = (BitVecGet(&bv, 0) == false);
        result      = result && (BitVecGet(&bv, 1) == true);
        result      = result && (BitVecGet(&bv, 2) == false);
        bool result = (BitVecGet(&bv, 0) == false);
        result      = result && (BitVecGet(&bv, 1) == true);
        result      = result && (BitVecGet(&bv, 2) == false);
    
        // Flip back
    
        // Should be back to original: 101
        result = result && (BitVecGet(&bv, 0) == true);
        result = result && (BitVecGet(&bv, 1) == false);
        result = result && (BitVecGet(&bv, 2) == true);
        // Should be back to original: 101
        result = result && (BitVecGet(&bv, 0) == true);
        result = result && (BitVecGet(&bv, 1) == false);
        result = result && (BitVecGet(&bv, 2) == true);
        result = result && (BitVecGet(&bv, 0) == true);
        result = result && (BitVecGet(&bv, 1) == false);
        result = result && (BitVecGet(&bv, 2) == true);
    
        // Clean up
    // Edge case tests
    bool test_bitvec_get_edge_cases(void) {
        WriteFmt("Testing BitVecGet edge cases\n");
    
        BitVec bv     = BitVecInit();
        // Test boundary conditions (no longer test empty bitvec - strict bounds checking now)
        BitVecPush(&bv, true);
        result = result && (BitVecGet(&bv, 0) == true); // Valid index
    
        // Test with large data set
        }
    
        result = result && (BitVecGet(&bv, 0) == true);             // First
        result = result && (BitVecGet(&bv, 999) == (999 % 3 == 0)); // Last
    
        result = result && (BitVecGet(&bv, 0) == true);             // First
        result = result && (BitVecGet(&bv, 999) == (999 % 3 == 0)); // Last
    
        BitVecDeinit(&bv);
        BitVecPush(&bv, false);
        BitVecSet(&bv, 0, true);
        result = result && (BitVecGet(&bv, 0) == true);
    
        // Test setting same value multiple times
        BitVecSet(&bv, 0, true);
        BitVecSet(&bv, 0, true);
        result = result && (BitVecGet(&bv, 0) == true);
    
        BitVecDeinit(&bv);
        BitVecPush(&bv, true);
        BitVecFlip(&bv, 0);
        result = result && (BitVecGet(&bv, 0) == false);
    
        BitVecFlip(&bv, 0);
    
        BitVecFlip(&bv, 0);
        result = result && (BitVecGet(&bv, 0) == true);
    
        BitVecDeinit(&bv);
            // Test access during growth
            if (cycle > 0) {
                result = result && (BitVecGet(&bv, 0) == true);
                result = result && (BitVecLen(&bv) == (size)(cycle + 1));
            }
            // Test setting and getting
            BitVecSet(&bv, cycle, cycle % 3 == 0);
            result = result && (BitVecGet(&bv, cycle) == (cycle % 3 == 0));
        }
                for (int bit = 0; bit < 8; bit++) {
                    bool expected = (pattern & (1u << bit)) != 0;
                    bool actual   = BitVecGet(&bv, base_idx + bit);
                    result        = result && (actual == expected);
                }
    
        // Test random access across the large dataset
        result = result && (BitVecGet(&bv, 0) == false);            // First bit of pattern
        result = result && (BitVecGet(&bv, 2) == true);             // Third bit of pattern
        result = result && (BitVecGet(&bv, bv.length - 1) == true); // Last bit
        // Test random access across the large dataset
        result = result && (BitVecGet(&bv, 0) == false);            // First bit of pattern
        result = result && (BitVecGet(&bv, 2) == true);             // Third bit of pattern
        result = result && (BitVecGet(&bv, bv.length - 1) == true); // Last bit
        result = result && (BitVecGet(&bv, 0) == false);            // First bit of pattern
        result = result && (BitVecGet(&bv, 2) == true);             // Third bit of pattern
        result = result && (BitVecGet(&bv, bv.length - 1) == true); // Last bit
    
        BitVecDeinit(&bv);
            u64  idx      = (test * 7 + test * 3) % size; // Pseudo-random indices
            bool expected = (idx / 7) % 2 == 0;
            bool actual   = BitVecGet(&bv, idx);
            result        = result && (actual == expected);
        }
        for (int i = 0; i < boundary_count; i++) {
            if (boundaries[i] < size) {
                bool original = BitVecGet(&bv, boundaries[i]);
                BitVecFlip(&bv, boundaries[i]);
                result = result && (BitVecGet(&bv, boundaries[i]) == !original);
                bool original = BitVecGet(&bv, boundaries[i]);
                BitVecFlip(&bv, boundaries[i]);
                result = result && (BitVecGet(&bv, boundaries[i]) == !original);
                BitVecFlip(&bv, boundaries[i]); // Flip back
                result = result && (BitVecGet(&bv, boundaries[i]) == original);
                result = result && (BitVecGet(&bv, boundaries[i]) == !original);
                BitVecFlip(&bv, boundaries[i]); // Flip back
                result = result && (BitVecGet(&bv, boundaries[i]) == original);
            }
        }
        // Verify all bits are false
        for (int i = 0; i < 100; i++) {
            result = result && (BitVecGet(&bv, i) == false);
        }
        for (int i = 0; i < 100; i++) {
            bool expected = (i % 2 == 0);
            result        = result && (BitVecGet(&bv, i) == expected);
        }
        BitVecPush(&bv, true); // F(1) = 1
        for (int i = 2; i < 50; i++) {
            bool prev1 = BitVecGet(&bv, i - 1);
            bool prev2 = BitVecGet(&bv, i - 2);
            BitVecPush(&bv, prev1 != prev2); // XOR
        for (int i = 2; i < 50; i++) {
            bool prev1 = BitVecGet(&bv, i - 1);
            bool prev2 = BitVecGet(&bv, i - 2);
            BitVecPush(&bv, prev1 != prev2); // XOR
        }
    
        // Verify first few Fibonacci bits
        result = result && (BitVecGet(&bv, 0) == true);  // F(0) = 1
        result = result && (BitVecGet(&bv, 1) == true);  // F(1) = 1
        result = result && (BitVecGet(&bv, 2) == false); // F(2) = 1 XOR 1 = 0
        // Verify first few Fibonacci bits
        result = result && (BitVecGet(&bv, 0) == true);  // F(0) = 1
        result = result && (BitVecGet(&bv, 1) == true);  // F(1) = 1
        result = result && (BitVecGet(&bv, 2) == false); // F(2) = 1 XOR 1 = 0
        result = result && (BitVecGet(&bv, 3) == true);  // F(3) = 1 XOR 0 = 1
        result = result && (BitVecGet(&bv, 0) == true);  // F(0) = 1
        result = result && (BitVecGet(&bv, 1) == true);  // F(1) = 1
        result = result && (BitVecGet(&bv, 2) == false); // F(2) = 1 XOR 1 = 0
        result = result && (BitVecGet(&bv, 3) == true);  // F(3) = 1 XOR 0 = 1
        result = result && (BitVecGet(&bv, 4) == true);  // F(4) = 0 XOR 1 = 1
        result = result && (BitVecGet(&bv, 1) == true);  // F(1) = 1
        result = result && (BitVecGet(&bv, 2) == false); // F(2) = 1 XOR 1 = 0
        result = result && (BitVecGet(&bv, 3) == true);  // F(3) = 1 XOR 0 = 1
        result = result && (BitVecGet(&bv, 4) == true);  // F(4) = 0 XOR 1 = 1
        result = result && (BitVecGet(&bv, 2) == false); // F(2) = 1 XOR 1 = 0
        result = result && (BitVecGet(&bv, 3) == true);  // F(3) = 1 XOR 0 = 1
        result = result && (BitVecGet(&bv, 4) == true);  // F(4) = 0 XOR 1 = 1
    
        BitVecDeinit(&bv);
    
        // Test NULL bitvec pointer - should abort
        BitVecGet(NULL, 0);
    
        return false;
    
        // Test get from empty bitvec - should abort
        BitVecGet(&bv, 0);
    
        BitVecDeinit(&bv);
    
        // Test with index way beyond length (3) - should abort
        BitVecGet(&bv, 1000);
    
        BitVecDeinit(&bv);
    
        // Test with maximum possible index value - should abort
        BitVecGet(&bv, SIZE_MAX);
    
        BitVecDeinit(&bv);
        // Check result should be: 101110
        result = result && (BitVecLen(&source) == 6);
        result = result && (BitVecGet(&source, 0) == true);
        result = result && (BitVecGet(&source, 1) == false);
        result = result && (BitVecGet(&source, 2) == true);
        result = result && (BitVecLen(&source) == 6);
        result = result && (BitVecGet(&source, 0) == true);
        result = result && (BitVecGet(&source, 1) == false);
        result = result && (BitVecGet(&source, 2) == true);
        result = result && (BitVecGet(&source, 0) == true);
        result = result && (BitVecGet(&source, 1) == false);
        result = result && (BitVecGet(&source, 2) == true);
    
        BitVecDeinit(&source);
        // Check result should be: 101110
        result = result && (BitVecLen(&source) == 6);
        result = result && (BitVecGet(&source, 0) == true);
        result = result && (BitVecGet(&source, 1) == false);
        result = result && (BitVecGet(&source, 2) == true);
        result = result && (BitVecLen(&source) == 6);
        result = result && (BitVecGet(&source, 0) == true);
        result = result && (BitVecGet(&source, 1) == false);
        result = result && (BitVecGet(&source, 2) == true);
        result = result && (BitVecGet(&source, 0) == true);
        result = result && (BitVecGet(&source, 1) == false);
        result = result && (BitVecGet(&source, 2) == true);
    
        BitVecDeinit(&source);
            if ((ValidateBitVec(UNPL(pbv)), 1) && UNPL(pbv)->length > 0)                                                   \
                for (u64 idx = 0, UNPL(d) = 1; UNPL(d); UNPL(d)--)                                                         \
                    for (bool var = 0; idx < UNPL(pbv)->length && (var = BitVecGet(UNPL(pbv), idx), 1); idx++)
    
    ///
                for (u64 idx = UNPL(pbv)->length; idx-- > 0 && idx < UNPL(pbv)->length;)                                   \
                    for (u8 UNPL(run_once) = 1; UNPL(run_once); UNPL(run_once) = 0)                                        \
                        for (bool var = BitVecGet(UNPL(pbv), idx); UNPL(run_once); UNPL(run_once) = 0)
    
    ///
                     UNPL(s) <= idx && idx < UNPL(e) && idx < UNPL(pbv)->length && UNPL(s) <= UNPL(e);                     \
                     ++idx, UNPL(d) = 1)                                                                                   \
                    for (bool var = BitVecGet(UNPL(pbv), idx); UNPL(d); UNPL(d) = 0)
    
Last updated on