• Home
  • 3.18 Bitwise NOT

3.18 Bitwise NOT

View Categories

3.18 Bitwise NOT

< 1 min read

Table of Contents

The bitwise NOT operator ~ inverts every bit of an integer operand. Each 0 bit becomes 1, and each 1 bit becomes 0.

#include <stdio.h>

int main()
{
    unsigned char number = 12;

    printf("number = %u\n", number);
    printf("~number = %u\n", (unsigned char)~number);

    return 0;
}

Example Output #

number = 12
~number = 243

Explanation #

The value 12 is represented using 8 bits as:

12 = 00001100

Applying the bitwise NOT operator inverts every bit:

    00001100
~ 00001100
--------
11110011


11110011 is 243 in decimal.

The cast:

(unsigned char)~number

keeps the result to the 8-bit unsigned char representation used by the example. Without the cast, the operand undergoes integer promotion before the ~ operation, so the result would be affected by the wider integer type.

Unlike logical NOT !, which produces only 0 or 1, bitwise NOT ~ operates on every bit of the integer operand.

Powered by BetterDocs

Leave a Reply

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