I have used bitmasks. There are only 16 possible Hamming codes. So, I have precalculated. Then check, whether input is a valid code. Then try to find such valid code that it differs from input in exactly one position (I have used xoring for that) and print that codr. Don't forget about zero code. That is a special case.