Bitwise NOT Operator (!~)
All the above three bitwise operators are binary operators (i.e, requiring two operands in order to operate). Unlike other bitwise operators, this one requires only one operand to operate.
The bitwise Not Operator takes a single value and returns its one’s complement. The one’s complement of a binary number is obtained by toggling all bits in it, i.e, transforming the 0 bit to 1 and the 1 bit to 0.
Example:
Take two bit values X and Y, where X = 5= (101)2 . Take Bitwise NOT of X.
Explanation: On the basis of truth table of bitwise NOT operator we can conclude that the result of
~1 = 0
~0 = 1We used the similar concept of bitwise operator that are show in the image.
Implementation of NOT operator:
#include <iostream>
using namespace std;
int main()
{
int a = 0;
cout << "Value of a without using NOT operator: " << a;
cout << "\nInverting using NOT operator (with sign bit): " << (~a);
cout << "\nInverting using NOT operator (without sign bit): " << (!a);
return 0;
}
/*package whatever //do not write package name here */
import java.io.*;
class GFG {
public static void main(String[] args)
{
int a = 0;
System.out.println(
"Value of a without using NOT operator: " + a);
System.out.println(
"Inverting using NOT operator (with sign bit): "
+ (~a));
if (a != 1)
System.out.println(
"Inverting using NOT operator (without sign bit): 1");
else
System.out.println(
"Inverting using NOT operator (without sign bit): 0");
}
}
// This code is contributed by lokesh.
a = 0
print("Value of a without using NOT operator: " , a)
print("Inverting using NOT operator (with sign bit): " , (~a))
print("Inverting using NOT operator (without sign bit): " , int(not(a)))
# This code is contributed by akashish__
using System;
public class GFG {
static public void Main()
{
int a = 0;
Console.WriteLine(
"Value of a without using NOT operator: " + a);
Console.WriteLine(
"Inverting using NOT operator (with sign bit): "
+ (~a));
if (a != 1)
Console.WriteLine(
"Inverting using NOT operator (without sign bit): 1");
else
Console.WriteLine(
"Inverting using NOT operator (without sign bit): 0");
}
}
// This code is contributed by akashish__
let a =0;
document.write("Value of a without using NOT operator: " + a);
document.write( "Inverting using NOT operator (with sign bit): " + (~a));
if(!a)
document.write( "Inverting using NOT operator (without sign bit): 1" );
else
document.write( "Inverting using NOT operator (without sign bit): 0" );
Output
Value of a without using NOT operator: 0 Inverting using NOT operator (with sign bit): -1 Inverting using NOT operator (without sign bit): 1
Time Complexity: O(1)
Auxiliary Space: O(1)
Introduction to Bitwise Algorithms – Data Structures and Algorithms Tutorial
Bit stands for binary digit. A bit is the basic unit of information and can only have one of two possible values that is 0 or 1. In our world, we usually with numbers using the decimal base. In other words. we use the digit 0 to 9 However, there are other number representations that can be quite useful such as the binary number systems.
Unlike humans, computers have no concepts of words and numbers. They receive data encoded at the lowest level as a series of zeros and ones (0 and 1). These are called bits, and they are the basis for all the commands they receive. We’ll begin by learning about bits and then explore a few algorithms for manipulating bits. We’ll then explore a few algorithms for manipulating bits. The tutorial is meant to be an introduction to bit algorithms for programmers.
Table of Content
- What is Bitwise Algorithms?
- Bitwise Operators / Basics of Bit manipulation
- Bitwise AND Operator (&)
- Bitwise OR Operator (|)
- Bitwise XOR Operator (^)
- Bitwise NOT Operator (!~)
- Left-Shift (<<)
- Right-Shift (>>)
- Application of Bit Operators
- Important Practice Problems on Bitwise Algorithm
Contact Us