VB in general is unforgiving with it's data types. I suspect that there is a casting problem with the function S with a and b. a and b are results of ANDing with a hex value, and subsequently could return a negative result in S. You're adding the results of S to an unsigned integer, and the two aren't compatible.
You might (but don't hold me to it) resolve this by appending UI to your hex values like this:
x And &HFFUI