Search⌘ K
AI Features

Bitwise NOT, Computations, and Examples

Explore how the bitwise NOT operator works by inverting each bit in a binary number, creating negative values through two's complement representation. This lesson helps you understand its computation, decimal conversion, and application in programming languages like Java, C, and C++.

Bitwise NOT or Bitwise complement (~)

We already discussed the NOT operator and how it inverts each input bit.

Now let’s see in-depth how to represent the output of each input and its decimal equivalent.

As the name suggests, it takes the input number, considers its binary representation, and inverts every bit, which means the 0 bit becomes 1, and the 1 bit becomes 0.

For example:

Let’s consider x = 1;

The binary representation of x as follows:

x  = 00000000 00000000 00000000 00000001

Now, Bitwise NOT of x will be:

~x = 11111111 11111111 11111111 11111110

So,

  • x contains 31 zeros(0's) and one 1
  • ~x contains 31 ones(1's) and one 0(zero)

So, how do we calculate the value of this? It is tough since we have 31 ones and 1 zero.

  • To guess the value, we must know how signed numbers are stored in a programming language.
  • Since, we know Java integers have the range from -2312^{31}
...