Bitwise NOT, Computations, and Examples
3 min readBitwise 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 00000001Now, Bitwise NOT of x will be:
~x = 11111111 11111111 11111111 11111110
So,
xcontains 31zeros(0's)and one1~xcontains 31ones(1's)and one0(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,648to2,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 "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 = -2Substitute 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 into1.
a = 1 => in Binary : 0000 0000 0000 0001
~a in Binary : 1111 1111 1111 1110Below 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 : -2Python
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 : -2C++
#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 : -2TypeScript
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 : -2Let’s jump into our first problem using the Bitwise ~ operator.