Note: i - U + {0 | -1 | 1} could be a negative number. In C++ the result of x%y is implementation-defined for negative values of x. You should make sure that your program gives back values that match your expectations.