[SOLVED] Why is `Not` implemeted for integral types?

Not has impls for all intregral types.. From testing it, it seems that !x = -x - 1. Why is this so? What is the use for this operation?

Edit: I meant Not not Neg, thanks @sfackler!

I'm assuming you mean Not, not Neg, right?

It is a bitwise not operation: Bitwise operation - Wikipedia

As far as use cases go, it's pretty heavily used when manipulating bitmasks. For example, to unset a flag you'd take my_mask & !the_flag.

It has the property you observed because this is exactly how 2's complement works: -x = !x + 1

1 Like