Export (0) Print
Expand All

One's Complement Operator: ~

~ cast-expression

The one's complement operator (~), sometimes called the "bitwise complement" operator, yields a bitwise one's complement of its operand. That is, every bit that is 1 in the operand is 0 in the result. Conversely, every bit that is 0 in the operand is 1 in the result. The operand to the one's complement operator must be an integral type.

Operator Keyword for ~

The compl operator is the text equivalent of ~. There are two ways to access the compl operator in your programs: include the header file iso646.h, or compile with /Za.

// expre_One_Complement_Operator.cpp
// compile with: /EHsc
#include <iostream>

using namespace std;

int main () {
   unsigned short y = 0xFFFF;
   cout << hex << y << endl;
   y = ~y;   // Take one's complement
   cout << hex << y << endl;

In this example, the new value assigned to y is the one's complement of the unsigned value 0xFFFF, or 0x0000.

Integral promotion is performed on integral operands, and the resultant type is the type to which the operand is promoted. See Integral Promotions for more information on how the promotion is done.

Community Additions

© 2015 Microsoft