The purpose of the tilde (~) in C language: to negate the operand bitwise, changing 0 to 1 and 1 to 0. Commonly used to negate a binary number or create a two's complement representation.
The tilde (~) in C language
In C language, the tilde (~) operation symbol means bitwise negation. It inverts each binary bit in the operand from 0 to 1 and from 1 to 0.
Usage
The tilde operator is mainly used for:
Syntax
The syntax of the tilde operator is as follows:
<code class="c">~表达式</code>
Example
The following example demonstrates how the tilde operator works:
<code class="c">int a = 5; // 二进制表示:0101 int b = ~a; // 二进制表示:1010 printf("a = %d\n", a); // 输出:5 printf("b = %d\n", b); // 输出:-6</code>
In this example, the binary representation of a is 0101. After applying the tilde operator (~a), each binary bit is inverted, resulting in 1010, which is the complement representation of -6.
Note
The tilde operator is a bitwise operator, which operates on the binary bits in the operand one by one. Therefore, it can only be used with integer types (char, int, long, etc.).
The above is the detailed content of What does the wavy line mean in C language?. For more information, please follow other related articles on the PHP Chinese website!