Skip to content

Bitwise NOT, Computations, and Examples

Bitwise NOT, 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.

Gopi Gorantala
Gopi Gorantala
3 min read

Introduction

This article teaches us 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.

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 -231 to 231-1, or -2,147,483,648 to 2,147,483,647.

In Java, positive numbers are stored by doing decimal to binary conversion.

For example, if we have the decimal number 3, then the binary is 11 with 30 0's on the left side. Nevertheless, negative numbers are stored in 2’s complement.

What is 2’s complement?

The rule of 2’s complement is:

  1. If the leading bit on the left side is 0, then it is a positive number.
  2. If the leading bit on the left side is 1, then it is a negative number.
  3. When doing NOT operation on positive numbers, they become negative and vice-versa.

Now, 2’s complement representation is

  • If we were given a negative number “-x”, its 2’s complement representation is "232 - x".

Formula

~x=(232 - x)

So, NOT(input) integer values can be identified using the above formula.

Let’s run some examples, so we understand this concept clearly.

a = 1
~a = -2

Substitute a value in the above formula

~a = (232 - a ) => (232 - 1)

Do the math of the above formula and convert decimal to binary. Whatever binary representation we get, that is the binary representation of -x. That is how negative numbers are calculated in Java.

We know the integer data type takes 4 bytes in C, C++, and Java.

4 Bytes = 4 * (8 bits) = 32 bits. { 1 Byte = 8 bits }

Let’s see one of the above examples in binary representation.

For simplicity, we’ll represent these in 16-bit instead of 32-bit binary.

a =  1 => in Binary : 0000 0000 0000 0001

        ~a in Binary : 1111 1111 1111 1110
 ----------------------------------------

So,  a = 1 becomes -2(~a)

The ~ operation happens from the rightmost bit traversing towards the leftmost bit in the 32 bit binary representation.

Let’s visualize the steps below:

Step #1:

  • Right, most bit inverts to 0
a =  1 => in Binary : 0000 0000 0000 0001

        ~a in Binary :                   0

Step #2:

  • All other bits are 0, and they invert into 1.
a =  1 => in Binary : 0000 0000 0000 0001

        ~a in Binary : 1111 1111 1111 1110

Below is a code representation of the ~ operator.

Code

Java

class NOTOperation {
    public static void main( String args[] ) {
        int a = 1;
        System.out.println("Bitwise NOT of a is : " + ~a);
    }
}

//Output : Bitwise NOT of a is : -2

Python

a=1
print("not operation of a is : ",~a)

JavaScript

const NotOperation = a => ~a;

const a = 1;
console.log(`Bitwise NOT of a is : ${NotOperation(a)}`);

//Output : Bitwise NOT of a is : -2

C++

#include <iostream>

using namespace std;

int main() {
    int a = 1;
    cout << "Bitwise NOT of a is : " << ~a;
    return 0;
}

//Output : Bitwise NOT of a is : -2

TypeScript

const NotOperation = (a: number): number => ~a;

const a: number = 1;
console.log(`Bitwise NOT of a is : ${NotOperation(a)}`);

// Output : Bitwise NOT of a is : -2

Let’s jump into our first problem using the Bitwise ~ operator.

Bit ManipulationData Structures and Algorithms

Gopi Gorantala Twitter

Gopi is an engineering leader with 12+ of experience in full-stack development—a specialist in Java technology stack. He worked for multiple startups, the European govt, and FAANG in India and Europe.

Comments


Related Posts

Members Public

Leetcode 217: Contains Duplicate

This question marks the first problem when working on duplicate data, either integers or strings etc. Companies that have asked this in their coding interview are Amazon, Apple, Netflix, Google, Microsoft, Adobe, Facebook, and many more top tech companies. Problem statement Given an integer array nums, return true if any

Leetcode 217: Contains Duplicate
Members Public

Leetcode 121: Best Time To Buy and Sell Stock

The Best time to buy and sell stock problem is a classic problem that can be solved using the Greedy approach. This is one of the most popular questions asked in such interviews. Companies that have asked this in their coding interview are Facebook, Amazon, Apple, Netflix, Google, Microsoft, Adobe,

Leetcode 121: Best Time To Buy and Sell Stock
Members Public

Bit Manipulation Course Overview

Overview In this course, you will learn how to solve problems using bit manipulation, a powerful technique that can be used to optimize your algorithmic and problem-solving skills. This is one of the most important/critical topics when someone starts preparing for coding interviews for FAANG companies. To kick things