The tilde (~) in C is used for bitwise inversion operation, inverting each binary bit of a variable or expression: (1) It converts a positive integer into a negative number, and a negative number into a positive number Number; (2) Convert the Boolean value true to false and vice versa.
The tilde (~) in C
~ is used for the unary operator in C, executing Bitwise negation operation.
Purpose:
~ The operator inverts each binary digit of a variable or expression. For integers, it converts positive numbers to negative numbers and negative numbers to positive numbers. For boolean values, it converts true to false and vice versa.
Syntax:
~expression
where expression is an integer, Boolean value, or bitmask to be inverted.
Example:
<code class="cpp">int num = 5; cout << ~num; // 输出:-6</code>
Bitwise negation rules:
~ The operator takes the binary bits according to the following rules Reverse:
Other uses: except
In addition to bitwise negation, ~ can also be used for:
* ~p
is equivalent to *(p - 1 )
, where p is a pointer to an object. ~ClassName
Represents the destructor of the class ClassName. The above is the detailed content of Usage of ~ in c++. For more information, please follow other related articles on the PHP Chinese website!