Can I turn negative number to positive with bitwise operations in Actionscript 3?

Use the rule that says

~(x) = (-x)-1

If two-complement is being used (usually the case), negation is complement then add 1:

-x == ~x + 1

Whether it's faster depends on what optimisations the compiler performs. When in doubt, test.


You need to add one after taking the bitwise negation. This is a property of two's complement number system. It is not related to Actionscript (aside from the alleged performance difference).

So, (~(-450)+1) gives 450
and (~(450)+1) gives -450.

As noted in comments, this answer is written in response to the question, to fix a minor issue in the question asker's experiment. This answer is not an endorsement of this technique for general software development use.