Bitwise NOT, Computations, and Examples

In this lesson, we learn more about the NOT operator and how it will be used in computers. We also learn the 2's complement and its formula.

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} to 2312^{31}- 1 or -2,147,483,648 to 2,147,483,647.

Level up your interview prep. Join Educative to access 80+ hands-on prep courses.