fix two's complement bug. 0xFF is -1, so it should be inverted instead
fixed bit masking bug again