Skip to content
BitVecIsSorted

BitVecIsSorted

Description

Check if bits in bitvector are in sorted order. Useful for certain algorithms and data structures.

Parameters

Name Direction Description
bv in Bitvector to check

Usage example (from documentation)

  bool sorted = BitVecIsSorted(&flags);

Success

true if bits are in non-decreasing order (0s before 1s)

Failure

false when any 0-bit follows a 1-bit in bv.

Usage example (Cross-references)

Usage examples (Cross-references)
    }
    
    bool BitVecIsSorted(const BitVec *bv) {
        ValidateBitVec(bv);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        WriteFmt("Testing BitVecIsSorted\n");
    
        BitVec bv = BitVecInit(ALLOCATOR_OF(&alloc));
    
        // Test empty bitvector (should be sorted)
        bool result = BitVecIsSorted(&bv);
    
        // Test sorted pattern: 0001111
        BitVecPush(&bv, true);
    
        result = result && BitVecIsSorted(&bv);
    
        // Test unsorted pattern (add 0 after 1s)
        // Test unsorted pattern (add 0 after 1s)
        BitVecPush(&bv, false);
        result = result && !BitVecIsSorted(&bv);
    
        // Test all zeros
            BitVecPush(&bv, false);
        }
        result = result && BitVecIsSorted(&bv);
    
        // Test all ones
            BitVecPush(&bv, true);
        }
        result = result && BitVecIsSorted(&bv);
    
        // Clean up
    
        // Test NULL pointer - should abort
        BitVecIsSorted(NULL);
    
        return false;
Last updated on