
- DSA - Home
- DSA - Overview
- DSA - Environment Setup
- DSA - Algorithms Basics
- DSA - Asymptotic Analysis
- Data Structures
- DSA - Data Structure Basics
- DSA - Data Structures and Types
- DSA - Array Data Structure
- DSA - Skip List Data Structure
- Linked Lists
- DSA - Linked List Data Structure
- DSA - Doubly Linked List Data Structure
- DSA - Circular Linked List Data Structure
- Stack & Queue
- DSA - Stack Data Structure
- DSA - Expression Parsing
- DSA - Queue Data Structure
- DSA - Circular Queue Data Structure
- DSA - Priority Queue Data Structure
- DSA - Deque Data Structure
- Searching Algorithms
- DSA - Searching Algorithms
- DSA - Linear Search Algorithm
- DSA - Binary Search Algorithm
- DSA - Interpolation Search
- DSA - Jump Search Algorithm
- DSA - Exponential Search
- DSA - Fibonacci Search
- DSA - Sublist Search
- DSA - Hash Table
- Sorting Algorithms
- DSA - Sorting Algorithms
- DSA - Bubble Sort Algorithm
- DSA - Insertion Sort Algorithm
- DSA - Selection Sort Algorithm
- DSA - Merge Sort Algorithm
- DSA - Shell Sort Algorithm
- DSA - Heap Sort Algorithm
- DSA - Bucket Sort Algorithm
- DSA - Counting Sort Algorithm
- DSA - Radix Sort Algorithm
- DSA - Quick Sort Algorithm
- Matrices Data Structure
- DSA - Matrices Data Structure
- DSA - Lup Decomposition In Matrices
- DSA - Lu Decomposition In Matrices
- Graph Data Structure
- DSA - Graph Data Structure
- DSA - Depth First Traversal
- DSA - Breadth First Traversal
- DSA - Spanning Tree
- DSA - Topological Sorting
- DSA - Strongly Connected Components
- DSA - Biconnected Components
- DSA - Augmenting Path
- DSA - Network Flow Problems
- DSA - Flow Networks In Data Structures
- DSA - Edmonds Blossom Algorithm
- DSA - Maxflow Mincut Theorem
- Tree Data Structure
- DSA - Tree Data Structure
- DSA - Tree Traversal
- DSA - Binary Search Tree
- DSA - AVL Tree
- DSA - Red Black Trees
- DSA - B Trees
- DSA - B+ Trees
- DSA - Splay Trees
- DSA - Range Queries
- DSA - Segment Trees
- DSA - Fenwick Tree
- DSA - Fusion Tree
- DSA - Hashed Array Tree
- DSA - K-Ary Tree
- DSA - Kd Trees
- DSA - Priority Search Tree Data Structure
- Recursion
- DSA - Recursion Algorithms
- DSA - Tower of Hanoi Using Recursion
- DSA - Fibonacci Series Using Recursion
- Divide and Conquer
- DSA - Divide and Conquer
- DSA - Max-Min Problem
- DSA - Strassen's Matrix Multiplication
- DSA - Karatsuba Algorithm
- Greedy Algorithms
- DSA - Greedy Algorithms
- DSA - Travelling Salesman Problem (Greedy Approach)
- DSA - Prim's Minimal Spanning Tree
- DSA - Kruskal's Minimal Spanning Tree
- DSA - Dijkstra's Shortest Path Algorithm
- DSA - Map Colouring Algorithm
- DSA - Fractional Knapsack Problem
- DSA - Job Sequencing with Deadline
- DSA - Optimal Merge Pattern Algorithm
- Dynamic Programming
- DSA - Dynamic Programming
- DSA - Matrix Chain Multiplication
- DSA - Floyd Warshall Algorithm
- DSA - 0-1 Knapsack Problem
- DSA - Longest Common Sub-sequence Algorithm
- DSA - Travelling Salesman Problem (Dynamic Approach)
- Hashing
- DSA - Hashing Data Structure
- DSA - Collision In Hashing
- Disjoint Set
- DSA - Disjoint Set
- DSA - Path Compression And Union By Rank
- Heap
- DSA - Heap Data Structure
- DSA - Binary Heap
- DSA - Binomial Heap
- DSA - Fibonacci Heap
- Tries Data Structure
- DSA - Tries
- DSA - Standard Tries
- DSA - Compressed Tries
- DSA - Suffix Tries
- Treaps
- DSA - Treaps Data Structure
- Bit Mask
- DSA - Bit Mask In Data Structures
- Bloom Filter
- DSA - Bloom Filter Data Structure
- Approximation Algorithms
- DSA - Approximation Algorithms
- DSA - Vertex Cover Algorithm
- DSA - Set Cover Problem
- DSA - Travelling Salesman Problem (Approximation Approach)
- Randomized Algorithms
- DSA - Randomized Algorithms
- DSA - Randomized Quick Sort Algorithm
- DSA - Karger’s Minimum Cut Algorithm
- DSA - Fisher-Yates Shuffle Algorithm
- Miscellaneous
- DSA - Infix to Postfix
- DSA - Bellmon Ford Shortest Path
- DSA - Maximum Bipartite Matching
- DSA Useful Resources
- DSA - Questions and Answers
- DSA - Selection Sort Interview Questions
- DSA - Merge Sort Interview Questions
- DSA - Insertion Sort Interview Questions
- DSA - Heap Sort Interview Questions
- DSA - Bubble Sort Interview Questions
- DSA - Bucket Sort Interview Questions
- DSA - Radix Sort Interview Questions
- DSA - Cycle Sort Interview Questions
- DSA - Quick Guide
- DSA - Useful Resources
- DSA - Discussion
DSA - Bitwise Algorithms
Introduction to Bitwise Algorithms
Bitwise algorithms are those that control the operation of individual bits of data. These algorithms are mainly used for enhancing operational speed and memory efficiency, especially when working with large data sets.
What is Bit?
Computers cannot understand the human language, it requires data encoded with bit. Here, Bit is the smallest unit of information in a computer. It is represented by binary digits that have only two values namely 0 and 1. Its other representations are YES or NO, TRUE or FALSE and ON or OFF.
The Bit Manipulation (operators)
Bit manipulation is a technique that involves performing low-level operations on individual bits of data, such as encrypting, toggling, shifting, or masking them. The operations performed on bit are called as bitwise operations. This operation requires one or two operands which are first converted to binary and then the operator is applied to each pair of corresponding bits. The outcome of this operation is also a binary number.
Bit manipulation can be useful for various purposes, such as −
It is useful in implementing low-level algorithms or data structures that require direct access to the binary representation of data, such as encryption, compression, hashing, or cryptography.
It can optimize the performance or memory usage by reducing the number of instructions or bytes needed to perform a given task, such as arithmetic, logic, or bit counting.
Bit manipulation is also used for manipulating hardware registers or device drivers that use specific bits to control the behavior or status of a device.
To perform Bit manipulation, we use various bitwise operators. They are explained below −
Bitwise AND operator (&)
The single ampersand sign (&) denotes the bitwise AND operator. It accepts two operands and returns 1 if both bits are 1, otherwise returns 0.

Example
In the following example, we are going to illustrate the AND operation in various programming languages.
#include <stdio.h> int main() { int valOne = 8; int valTwo = 9; int output = valOne & valTwo; printf("Result of AND operation: %d\n", output); return 0; }
#include <iostream> using namespace std; int main() { int valOne = 8; int valTwo = 9; int output = valOne & valTwo; cout << "Result of AND operation: " << output << endl; return 0; }
public class Main { public static void main(String[] args) { int valOne = 8; int valTwo = 9; int output = valOne & valTwo; System.out.println("Result of AND operation: " + output); } }
valOne = 8 valTwo = 9 output = valOne & valTwo print("Result of AND operation:", output)
Output
Result of AND operation: 8
Bitwise OR operator (|)
The single pipe sign (|) denotes the bitwise OR operator. It accepts two operands as a parameter value and returns 1 if either bit is 1, otherwise it returns 0.

Example
Following is the example that demonstrates the working of bitwise OR operator in different programming languages.
#include <stdio.h> int main() { int valOne = 8; int valTwo = 9; int output = valOne | valTwo; printf("Result of OR operation: %d\n", output); return 0; }
#include <iostream> using namespace std; int main() { int valOne = 8; int valTwo = 9; int output = valOne | valTwo; cout << "Result of OR operation: " << output << endl; return 0; }
public class Main { public static void main(String[] args) { int valOne = 8; int valTwo = 9; int output = valOne | valTwo; System.out.println("Result of OR operation: " + output); } }
valOne = 8 valTwo = 9 output = valOne | valTwo print("Result of OR operation:", output)
Output
Result of OR operation: 9
Bitwise XOR operator (^)
The bitwise XOR operator is denoted by the Circumflex symbol (^). It also accepts two operands and returns 1 if the bits are different, otherwise it returns 0.

Example
The following example shows how bitwise XOR operator works.
#include <stdio.h> int main() { int valOne = 8; int valTwo = 9; int output = valOne ^ valTwo; printf("Result of XOR operation: %d\n", output); return 0; }
#include <iostream> using namespace std; int main() { int valOne = 8; int valTwo = 9; int output = valOne ^ valTwo; cout << "Result of XOR operation: " << output << endl; return 0; }
public class Main { public static void main(String[] args) { int valOne = 8; int valTwo = 9; int output = valOne ^ valTwo; System.out.println("Result of XOR operation: " + output); } }
valOne = 8 valTwo = 9 output = valOne ^ valTwo print("Result of XOR operation:", output)
Output
Result of XOR operation: 1
Bitwise NOT operator (~)
The bitwise NOT operator is denoted by a single tilde sign (~). It accepts either 1 or 0 as an operand and returns the complement of that operand, which means it flips every bit from 0 to 1 and 1 to 0.

Example
Following is the example illustrating the working of NOT operator in various programming languages.
#include <stdio.h> int main() { int value = 0; int output = ~value; printf("Result of NOT operation: %d\n", output); return 0; }
#include <iostream> using namespace std; int main() { int value = 0; int output = ~value; cout << "Result of NOT operation: " << output << endl; return 0; }
public class Main { public static void main(String[] args) { int value = 0; int output = ~value; System.out.println("Result of NOT operation: " + output); } }
value = 0 output = ~value print("Result of NOT operation:", output)
Output
Result of NOT operation: -1
Left-Shift operator (<<)
The double left arrow symbol (<<) denotes the left shift operator. It is used to shift the specified bits of the operand to the left by a given number of positions. It fills the vacated bits with zeros.

Example
In the following example, we are going to illustrate the left-shift operation in various programming languages.
#include <stdio.h> int main() { int value = 11; // int to binary conversion char newVal[5]; for(int i = 3; i >= 0; i--) { newVal[3-i] = ((value >> i) & 1) ? '1' : '0'; } newVal[4] = '\0'; int output = value << 2; printf("Binary representation of value: %s\n", newVal); printf("Result of Left-Shift operation: %d\n", output); return 0; }
#include <iostream> #include <bitset> using namespace std; int main() { int value = 11; // int to binary conversion string newVal = bitset<4>(value).to_string(); int output = value << 2; cout << "Binary representation of value: " << newVal << endl; cout << "Result of Left-Shift operation: " << output << endl; return 0; }
public class Main { public static void main(String[] args) { int value = 11; // int to binary conversion String newVal = Integer.toBinaryString(value); int output = value << 2; System.out.println("Binary representation of value: " + newVal); System.out.println("Result of Left-Shift operation: " + output); } }
value = 11 # int to binary conversion newVal = format(value, '04b') output = value << 2 print("Binary representation of value:", newVal) print("Result of Left-Shift operation:", output)
Output
Binary representation of value: 1011 Result of Left-Shift operation: 44
Right-Shift operator (>>)
The double right arrow symbol (>>) denotes the right shift operator. It is used to shift the specified bits of the operand to the right by a given number of positions. It fills the vacated bits with either zeros or the sign bit (depending on whether the operand is signed or unsigned).

Example
The following example demonstrates the working of Right-Shift operation in various programming languages.
#include <stdio.h> int main() { int value = 11; // int to binary conversion char newVal[5]; for(int i = 3; i >= 0; i--) { newVal[3-i] = ((value >> i) & 1) ? '1' : '0'; } newVal[4] = '\0'; int output = value >> 2; printf("Binary representation of value: %s\n", newVal); printf("Result of Right-Shift operation: %d\n", output); return 0; }
#include <iostream> #include <bitset> using namespace std; int main() { int value = 11; // int to binary conversion string newVal = bitset<4>(value).to_string(); int output = value >> 2; cout << "Binary representation of value: " << newVal << endl; cout << "Result of Right-Shift operation: " << output << endl; return 0; }
public class Main { public static void main(String[] args) { int value = 11; // int to binary conversion String newVal = Integer.toBinaryString(value); int output = value >> 2; System.out.println("Binary representation of value: " + newVal); System.out.println("Result of Right-Shift operation: " + output); } }
value = 11 # int to binary conversion newVal = format(value, '04b') output = value >> 2 print("Binary representation of value:", newVal) print("Result of Right-Shift operation:", output)
Output
Binary representation of value: 1011 Result of Right-Shift operation: 2