You could put the code portions of your answer in a gist/pastebin/whatever containing a license and link to that from your answer text. Your answer text would then be CC-BY-SA, but you could license your code under GPLv3 or whatever license you fancy.