No. of blocks of main Memory $= \dfrac{2^{32}}{2^5} = 2^{27}$
And there are $512 = 2^9$ lines in Cache Memory.
Tag bits tell us to how many blocks does $1$ line in Cache memory points to
$1$ cache line points to $ \large \dfrac{2^{27}}{2^9} = 2^{18}$ lines
So, $18$ bits are required as TAG bits.