As per the berne convention, You have the copyright to any Intellectual property you produce (including source code).
By "protect my code", I assume you want to distribute your code (source or binaries) and impose usage restrictions. For that you have to release your code under a License that you will choose. There are a number of Licenses (CC/GPL/LGPL/Mozilla/Eclipse etc) and you can write your own as well
If you have some new proprietary algos/tools/techniques and you dont want to make them public but ensure no one will use them without your permission, then Patent is the way to go (There is a lot of drama associated with patents, but that's out of the scope right now)