Home > Backend Development > C++ > Usage of ~ in c++

Usage of ~ in c++

下次还敢
Release: 2024-04-26 18:48:14
Original
561 people have browsed it

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.

Usage of ~ in c++

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>
Copy after login

Bitwise negation rules:

~ The operator takes the binary bits according to the following rules Reverse:

  • 0 is converted to 1
  • 1 is converted to 0

Other uses: except

In addition to bitwise negation, ~ can also be used for:

  • Pointer dereference: * ~p is equivalent to *(p - 1 ), where p is a pointer to an object.
  • Get the destructor of the class: ~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!

Related labels:
c++
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template