def z2_expansion(n, N, n_a, n_b):
    """
    Performs so-called Z2-symmetry expansion on a parity representation.
    
    :param n: any positive integer
    :param N: number of bits/qubits used in representing the integer `n`
    :param n_a: the number of alpha electrons
    :param n_b: the number of beta spin electrons
    :returns: the integer representing the parity value after expansion
    """
    lower_mask = 2 ** (N//2) - 1
    upper_mask = lower_mask << N//2
    
    z2_expanded = (n & lower_mask) + (((n_a) % 2) << N//2) + ((n & upper_mask) << 1) + (((n_a + n_b) % 2) << (N + 1))
    
    #print(f'{n} = {n:0{N}b} : {z2_expanded:0{N+2}b} {lower_mask:0{N+2}b} {upper_mask:0{N+2}b}')
    return z2_expanded