def num_alpha(n, N):
    """
    Counts the number of alpha electrons in a Fock representation. Assumes that the orbitals are sorted by spin first.
    
    :param n: any positive integer
    :param N: number of bits/qubits used in representing the integer `n`
    :returns: an integer giving the number of alpha electrons
    """
    lower_mask = (2 ** (N//2) - 1)
    masked = (n & lower_mask)
    counter = 0
    indexer = 1
    for i in range(N//2):
        counter += bool(masked & indexer)
        indexer = indexer << 1
        
    return counter