def _count_trailing_zeros(mask: int) -> int:
    """count the trailing zeros of a bit mask. Used for shifting.

    Args:
        mask (int): bit mask, eg 0b00111000

    Returns:
        int: number of trailing zeros
    """
    if mask == 0:
        raise ValueError("mask is all zeros")
    count = 0
    for i in range(mask.bit_length()):
        if mask & 1:
            return count
        count += 1
        mask >>= 1
    return count