I don't like this. If you're just trying to decide if one is contained by the other or not, you can use compareDocumentPosition() for that. Read the docs. Other bits are set in the contains or contained by cases.

And you're changing the definition of "before" and "after" here with this change. Is that safe everywhere that these functions are used?