Use defensive programming

(~P % 2) may be -1 if P is of type int whereas (~P & 1) is either 0 or 1 for both int and unsigned