def mat2bits(A, trans=False):
    """
    Convert a matrix into a list of bits.

    The bits are taken from the matrix column by column with keys in sorted order

    Keyword arguments:
    trans -- whether to reverse rows and columns of the matrix (default False)
    """
    if trans:
        return [A[i,j] for i in sorted(A.D[0]) for j in sorted(A.D[1])]
    else:
        return [A[i,j] for j in sorted(A.D[1]) for i in sorted(A.D[0])]