Fixed types and simplify some asserts.
Add local optimization: ~x - y --> ~(x + y).