From the Shulchan Aruch Ha'Rav Siman 85, Sif 4:
"One should think divrei Torah even in a bathroom, if that's the only way to keep thoughts of women out of his head"