You should be using &0xFFFF for masking. Your mod is off by 1 too.
You're right, the code was correct but my comment above is wrong.