OK here's how you can solve this problem.
Start by representing p and q in terms of two k-bit numbers r and s as follows (for your example, k=512):

p = 2kr + s
q = 2ks + r

The value of n is the product of these two numbers:

n   =   pq   =   (2kr + s)(2ks + r)   =   22krs + 2k(r2 + s2) + rs

The first two terms on the right are both multiples of 2k, so the k lowest bits of n are exactly equal to the k lowest bits of rs. Furthermore, since rs is typically a 2k-bit number and r2 + s2 is typically a (2k+1)-bit number, the k highest bits of n are also mostly equal to the k highest bits of rs, but perhaps slightly larger due to the carry generated when adding the 2k(r2 + s2) term.
If n◁ and n▷ are numbers representing the top k bits and bottom k bits of n, then we can generate a candidate value for rs by calculating 2kn◁ + n▷. If this value is correct, we can subtract (22k + 1)rs from n to obtain the value of 2k(r2 + s2). Divide this result by 2k and add 2rs to obtain r2 + 2rs + s2, then calculate the square root of this value to obtain the value of r + s. (If the number isn't a perfect square, then you need to subtract 1 from n◁ and try again.
After at most two iterations of this process, you will have the exact values of rs and r+s. You should then have no difficulty solving a simultaneous equation to obtain values for r and s, from which you can find p and q.
Note: You might find the sympy.sqrt() function useful for calculating square roots of large numbers. It returns objects with an is_integer attribute that will tell you if the number you provided was a perfect square.
I'm using the RSA encryption/decryption system, and I have the modulus n (which is a 2048 bit number) and I need to find p and q, which satisfy n = p*q and both are prime numbers. The clue that is given to me is that p is equal to q but with its bits inverted as I say in the title of this post (concretely r and s have the same bits so we could say that p and q have their halves inverted). I don't find the way to take advantage of this so I would be very grateful if someone could help me
I have tried to traverse the number n to find the number p that satisfies that p * p_halfs_inverted = n but logically n is too huge and it is not viable to do it in this way.
Having n (2048 bit number), how can I find two numbers p and q that satisfy n = p*q, where p = r||s (r and s concatenated) and q = s||r?