• Home
  • 3.17 Bitwise NOT Operator (~)

3.17 Bitwise NOT Operator (~)

View Categories

3.17 Bitwise NOT Operator (~)

< 1 min read

Table of Contents

The bitwise NOT operator (~) is a unary operator that inverts every bit of its operand. Each bit with the value 1 becomes 0, and each bit with the value 0 becomes 1.

The bitwise NOT operator is commonly used in bit manipulation tasks such as creating bit masks and clearing selected bits. It operates only on integral data types.

Source Code #

#include <iostream>

int main()
{
    int number = 12;

    int result = ~number;

    std::cout << "Number : " << number << '\n';
    std::cout << "Result : " << result << '\n';

    return 0;
}

Output #

Number : 12
Result : -13

Explanation #

  • The bitwise NOT operator (~) is a unary operator that operates on a single integral operand.
  • It inverts every bit of the operand by changing 1 bits to 0 and 0 bits to 1.
  • The binary representation of 12 is:12 = 00000000 00000000 00000000 00001100
  • After applying the bitwise NOT operator:~12 = 11111111 11111111 11111111 11110011
  • On most modern systems that use two’s complement representation, the resulting bit pattern represents the decimal value -13.
  • The exact binary representation depends on the size of the integer type, but the numerical result is typically -13 for a 32-bit int.
  • Bitwise NOT is commonly used when constructing masks to clear or invert selected bits.
  • return 0; terminates the program successfully.

Powered by BetterDocs

Leave a Reply

Your email address will not be published. Required fields are marked *