The shell structure underlying shape changes in neutron-rich nuclei between N=20 and N=28 has been investigated by a novel application of the transient field technique to measure the first-excited state g factors in S-38 and S-40 produced as fast radioactive beams. Details of the new methodology are presented. In both S-38 and S-40 there is a fine balance between the proton and neutron contributions to the magnetic moments. Shell model calculations which describe the level schemes and quadrupole properties of these nuclei also give a satisfactory explanation of the g factors. In S-38 the g factor is extremely sensitive to the occupation of the neutron p3/2 orbit above the N=28 shell gap as occupation of this orbit strongly affects the proton configuration. The g factor of deformed S-40 does not resemble that of a conventional collective nucleus because spin contributions are more important than usual.