The Witten effect predicts that a magnetic monopole acquires a fractional electric charge inside topological insulators. In this work, we give a microscopic description of this phenomenon as well as an analogous two-dimensional system with a vortex. We solve the Dirac equation of electron field both analytically in a continuum and numerically on a lattice by adding the Wilson term and smearing the gauge field within a finite range to regularize the short-distance behavior of the system.