# 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.

## 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 -2
^{31}to 2^{31}-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:

- If the leading bit on the left side is 0, then it is a positive number.
- If the leading bit on the left side is 1, then it is a negative number.
- 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 "2
^{32}- x".

## Formula

~x=(2

^{32}- 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 = (2

^{32}- a ) => (2^{32}- 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.

## Gopi Gorantala Newsletter

Join the newsletter to receive the latest updates in your inbox.