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


#1

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!


#2

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

It is a bitwise not operation: https://en.wikipedia.org/wiki/Bitwise_operation#NOT


#3

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.


#4

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