# ~ - bitwise NOT (unary complement)

suggest change
``````#include <iostream>

int main(int argc, char **argv) {
unsigned char a = 234;  // 1110 1010b  (0xEA)
unsigned char b = ~a;   // 0001 0101b  (0x15)

std::cout << "a = " << static_cast<int>(a) <<
", b = " << static_cast<int>(b) << std::endl;
}``````
``````a = 234, b = 21
``````

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:

``````#include <iostream>

int main(int argc, char **argv) {
unsigned int i = ~0;
unsigned char c = ~0;

std::cout << "max uint = " << i << std::endl <<
"max uchar = " << static_cast<short>(c) << std::endl;
}``````
``````max uint = 4294967295
max uchar = 255
``````

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)`