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 ", 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 ", 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 ", 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 ", 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 ", newVal); printf("Result of Left-Shift operation: %d ", 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 ", newVal); printf("Result of Right-Shift operation: %d ", 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