https://codeeval.dev/gist/d901754eaed9580f0af902ca32f9c1be

Why

A bit wise NOT (unary complement) operates on the bit level and simply flips each bit. If it’s a 1, it’s changed to a 0, if it’s a 0, it’s changed to a 1. The bit wise NOT has the same effect as XOR’ing a value against the max value for a specific type:

unsigned char a = 234;  // 1110 1010b  (0xEA)
unsigned char b = ~a;   // 0001 0101b  (0x15)
unsigned char c = a ^ ~0;

The bit wise NOT can also be a convenient way to check the maximum value for a specific integral type:

https://codeeval.dev/gist/83a75e434c1e80faa5ed832457b85ec6

The bit wise NOT does not change the value of the original value and does not have a compound assignment operator, so you can not do a ~= 10 for example.

The bit wise NOT (~) should not be confused with the logical NOT (\\!); where a bit wise NOT will flip each bit, a logical NOT will use the whole value to do its operation on, in other words (!1) != (~1)