def count_half_transpositions(s1, s2, flags1, flags2):
    """Count the number of half transpositions between two strings.

    A half transposition, roughly defined, is two characters, taken from the
    sequence of paired matched chars, which are unequal to each other.

    The arguments 'flags1' and 'flags2' are the bit arrays produced by the
    count_matches() function."""
    half_transposes = 0
    k = 0
    for i, flag in enumerate(flags1):
        if not flag:
            continue
        while not flags2[k]:
            k += 1
        if s1[i] != s2[k]:
            half_transposes += 1
        k += 1
    return half_transposes
