The Wayback Machine - https://web.archive.org/web/20241001144018/https://www.geeksforgeeks.org/bitwise-algorithms/
Open In App

Bitwise Algorithms

Last Updated : 23 Aug, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Bitwise algorithms in Data Structures and Algorithms (DSA) involve manipulating individual bits of binary representations of numbers to perform operations efficiently. These algorithms utilize bitwise operators like AND, OR, XOR, shift operators, etc., to solve problems related to tasks such as setting, clearing, or toggling specific bits, checking if a number is even or odd, swapping values without using a temporary variable, and more. Bitwise algorithms are crucial in optimizing code for speed and memory usage in various programming scenarios.

What are Bitwise Algorithms?

Bitwise algorithms are algorithms that operate on individual bits of data rather than on larger data types like integers or floating-point numbers. These algorithms manipulate bits directly, typically using bitwise operators such as AND, OR, XOR, shift left, shift right, and complement.

Common Bitwise Algorithms and Operations:

Here are some common bitwise algorithms and operations:

  • Bitwise AND (&): Takes two numbers as input and performs a bitwise AND operation on their corresponding bits. It returns 1 only if both bits are 1; otherwise, it returns 0.
  • Bitwise OR (|): Performs a bitwise OR operation on the corresponding bits of two numbers. It returns 1 if at least one of the bits is 1.
  • Bitwise XOR (^): Performs a bitwise exclusive OR operation on the corresponding bits of two numbers. It returns 1 if the bits are different and 0 if they are the same.
  • Bitwise NOT (~): Performs a bitwise NOT operation, which flips each bit of the input (1 becomes 0 and 0 becomes 1).
  • Left Shift (<<) and Right Shift (>>): These operators shift the bits of a number to the left or right by a specified number of positions. Left shifting is equivalent to multiplying the number by 2, while right shifting is equivalent to dividing by 2.

Applications of Bitwise Algorithms:

  • Bit manipulation (setting, clearing, toggling bits): Bitwise operators are often used to manipulate individual bits of numbers. This includes tasks such as setting bits (using OR), clearing bits (using AND with the complement), toggling bits (using XOR with 1), and checking the value of a specific bit.
  • Efficient storage of data: Bitwise algorithms play a crucial role in data compression techniques like Huffman coding. They can efficiently represent and process compressed data by manipulating bits directly.
  • Cryptography: Many cryptographic algorithms, such as AES (Advanced Encryption Standard), DES (Data Encryption Standard), and SHA (Secure Hash Algorithm), utilize bitwise operations for encryption, decryption, and hashing. Bitwise XOR, in particular, is commonly used in encryption algorithms for its simplicity and effectiveness.
  • Networking and Protocol Handling: Bitwise algorithms are used in networking protocols for tasks like IP address manipulation, subnet masking, and packet parsing. For example, bitwise AND is used in subnet masking to determine the network address from an IP address and subnet mask.
  • Low-Level System Programming: Bitwise operations are essential in low-level system programming for tasks such as device control, memory management, and bit-level I/O operations. They are used to manipulate hardware registers, set/clear flags, and optimize code for performance.
  • Error Detection and Correction: Bitwise algorithms are employed in error detection and correction techniques, such as CRC (Cyclic Redundancy Check) and Hamming codes. These algorithms use bitwise XOR and other operations to detect and correct errors in transmitted data.

Basics of Bitwise Algorithms:

Bit Manipulation Tips and Tricks

Easy Problems on Bit Algorithms:

Medium Problems on Bit Algorithms:

Hard Problems on Bit Algorithms:

Quick Links :



Similar Reads

Total pairs in an array such that the bitwise AND, bitwise OR and bitwise XOR of LSB is 1
Given an array arr[] of size N. The task is to find the number of pairs (arr[i], arr[j]) as cntAND, cntOR, and cntXOR such that: cntAND: Count of pairs where bitwise AND of least significant bits is 1.cntOR: Count of pairs where bitwise OR of least significant bits is 1.cntXOR: Count of pairs where bitwise XOR of least significant bits is 1. Exampl
7 min read
Maximize count of pairs whose Bitwise AND exceeds Bitwise XOR by replacing such pairs with their Bitwise AND
Given an array arr[] consisting of N positive integers, replace pairs of array elements whose Bitwise AND exceeds Bitwise XOR values by their Bitwise AND value. Finally, count the maximum number of such pairs that can be generated from the array. Examples: Input: arr[] = {12, 9, 15, 7}Output: 2Explanation:Step 1: Select the pair {12, 15} and replac
9 min read
Calculate Bitwise OR of two integers from their given Bitwise AND and Bitwise XOR values
Given two integers X and Y, representing Bitwise XOR and Bitwise AND of two positive integers, the task is to calculate the Bitwise OR value of those two positive integers. Examples: Input: X = 5, Y = 2 Output: 7 Explanation: If A and B are two positive integers such that A ^ B = 5, A & B = 2, then the possible value of A and B is 3 and 6 respe
7 min read
Bitwise OR of Bitwise AND of all subsets of an Array for Q queries
Given two arrays arr[] of size N and queries[] of size Q, the task is to find the OR of AND of subsets of the array. In each query, you are given an index and a value, you have to replace the value at the given index of the arrays with a given value and print the OR of AND of all subsets of the array after each query. Examples: Input: arr[] = {3, 5
9 min read
Find subsequences with maximum Bitwise AND and Bitwise OR
Given an array of n elements. The task is to print the maximum sum by selecting two subsequences of the array (not necessarily different) such that the sum of bitwise AND of all elements of the first subsequence and bitwise OR of all the elements of the second subsequence is maximum. Examples: Input: arr[] = {3, 5, 6, 1} Output: 13 We get maximum A
4 min read
Find pair whose bitwise OR and bitwise AND follows the given condition
Given 2 arrays arr1[] and arr2[], of size N and M respectively, the task is to find a pair of value (say a and b) such that the following conditions are satisfied: (a | b) ? arr1[i] for all values of i in the range [0, N-1].(a & b) ? arr2[j] for all values of j in the range [0, M-1]. Examples: Input: arr1[] = { 6, 9, 7, 8}, arr2[] = {2, 1, 3, 4
7 min read
Minimum possible Bitwise OR of all Bitwise AND of pairs generated from two given arrays
Given two arrays arr[] and brr[] of length N and M respectively, create an array res[] of length N using the below operations such that the Bitwise OR of all the elements in the array res[] is minimum. For every index i in arr[], choose any index j(repetitions allowed) from the array brr[] and update res[i] = arr[i] & brr[j] The task is to prin
9 min read
Count ways to generate pairs having Bitwise XOR and Bitwise AND equal to X and Y respectively
Given two integers X and Y, the task is to find the total number of ways to generate a pair of integers A and B such that Bitwise XOR and Bitwise AND between A and B is X and Y respectively Examples: Input: X = 2, Y = 5Output: 2Explanation:The two possible pairs are (5, 7) and (7, 5).Pair 1: (5, 7)Bitwise AND = 5 & 7 = 2Bitwise XOR = 5 ^ 7 = 5P
7 min read
Count pairs from an array whose Bitwise OR is greater than Bitwise AND
Given an array A[] consisting of N integers, the task is to count the number of pairs (i, j) such that i < j, and Bitwise OR of A[i] and A[j] is greater than Bitwise AND of A[i] and A[j]. Examples: Input: A[] = {1, 4, 7}Output: 3Explanation: There are 3 such pairs: (1, 4), (4, 7), (1, 7).1) 1 | 4 (= 5) > 1 & 4 (= 0)2) 4 | 7 (= 7) > 4
10 min read
Count pairs with equal Bitwise AND and Bitwise OR value
Given an array, arr[] of size N, the task is to count the number of unordered pairs such that Bitwise AND and Bitwise OR of each pair is equal. Examples: Input: arr[] = {1, 2, 1} Output: 1 Explanation: Bitwise AND value and Bitwise OR value all possible pairs are: Bitwise AND of the pair(arr[0], arr[1]) is (arr[0] & arr[1]) = (1 & 2) = 0 Bi
6 min read
Count pairs with bitwise XOR exceeding bitwise AND from a given array
Given an array, arr[] of size N, the task is to count the number of pairs from the given array such that the bitwise AND(&) of each pair is less than its bitwise XOR(^). Examples: Input: arr[] = {1, 2, 3, 4, 5} Output: 8Explanation: Pairs that satisfy the given conditions are: (1 & 2) < (1 ^ 2)(1 & 3) < (1 ^ 3)(1 & 4) < (1
10 min read
Non-negative pairs with sum of Bitwise OR and Bitwise AND equal to N
Given an integer N, the task is to find all non-negative pairs (A, B) such that the sum of Bitwise OR and Bitwise AND of A, B is equal to N, i.e., (A | B) + (A & B) = N. Examples: Input: N = 5Output: (0, 5), (1, 4), (2, 3), (3, 2), (4, 1), (5, 0)Explanation: All possible pairs satisfying the necessary conditions: (0 | 5) + (0 & 5) = 5 + 0 =
5 min read
Find the triplet from given Bitwise XOR and Bitwise AND values of all its pairs
Given six positive integers representing the Bitwise XOR and Bitwise AND of all possible pairs of a triplet (a, b, c), the task is to find the triplet. Examples: Input: aXORb = 30, aANDb = 0, aXORc = 10, aANDc = 20, aXORb = 20, aANDb = 10 Output: a = 10, b = 20, c= 30 Explanation: If a = 10, b = 20, c= 30 a ^ b = 30, a & b = 0 a ^ c = 10, a
7 min read
Sum of Bitwise AND of sum of pairs and their Bitwise AND from a given array
Given an array arr[] consisting of N integers, the task is to find the sum of Bitwise AND of (arr[i] + arr[j]) and Bitwise AND of arr[i] and arr[j] for each pair of elements (arr[i], arr[j]) from the given array. Since the sum can be very large, print it modulo (109 + 7). Examples: Input: arr[] = {8, 9}Output: 0Explanation: The only pair from the a
9 min read
Maximize count of pairs whose bitwise XOR is even by replacing such pairs with their Bitwise XOR
Given an array arr[] of size N, the task is to replace a pair of array elements whose Bitwise XOR is even by their Bitwise XOR. Repeat the above step as long as possible. Finally, print the count of such operations performed on the array Examples: Input: arr[] = { 4, 6, 1, 3 }Output: 3Explanation:Step 1: Remove the pair (4, 6) and replace them by t
11 min read
Count pairs with odd Bitwise XOR that can be removed and replaced by their Bitwise OR
Given an array arr[] consisting of N integers, the task is to count the number of pairs whose Bitwise XOR is odd, that can be removed and replaced by their Bitwise OR values until no such pair exists in the array. Examples: Input: arr[] = {5, 4, 7, 2}Output: 2Explanation:Pair (5, 4): Bitwise XOR of 5 and 4 is 1. Remove this pair and add their Bitwi
5 min read
Split an array into subarrays with maximum Bitwise XOR of their respective Bitwise OR values
Given an array arr[] consisting of N integers, the task is to find the maximum Bitwise XOR of Bitwise OR of every subarray after splitting the array into subarrays(possible zero subarrays). Examples: Input: arr[] = {1, 5, 7}, N = 3Output: 7Explanation:The given array can be expressed as the 1 subarray i.e., {1, 5, 7}.The Bitwise XOR of the Bitwise
9 min read
Minimum bit swaps between given numbers to make their Bitwise OR equal to Bitwise AND
Given two positive integers A and B, the task is to calculate the minimum number of operations required such that Bitwise OR of A and B equals Bitwise AND of A and B are equal i.e (A&B)=(A|B), where, in each operation two indices i and j are chosen and the ith bit of A is swapped with the jth bit of B. If it is not possible to make (A&B)=(A
9 min read
Bitwise XOR of Bitwise AND of all pairs from two given arrays
Given two arrays arr1[] and arr2[] consisting of N and M integers respectively, the task is to print the Bitwise XOR of Bitwise AND of all pairs possible by selecting an element from arr1[] and arr2[]. Examples: Input: arr1[] = {1, 2, 3}, arr2[] = {6, 5}Output: 0Explanation: Bitwise AND of the pair (arr1[0], arr2[]) = 1 & 6 = 0.Bitwise AND of t
10 min read
Bitwise OR of Bitwise AND of all subarrays of an array
Given an array arr[] consisting of N positive integers, the task is to find the Bitwise OR of Bitwise AND of all subarrays of the given arrays. Examples: Input: arr[] = {1, 2, 3}Output: 3Explanation:The following are Bitwise AND of all possible subarrays are: {1}, Bitwise AND is 1.{1, 2}, Bitwise AND is 0.{1, 2, 3}, Bitwise AND is 0.{2}, Bitwise AN
7 min read
Find size of largest subset with bitwise AND greater than their bitwise XOR
Given an array arr[] of N integers, the task is to find the size of the largest subset such that the bitwise AND of all elements of the subset is greater than the bitwise XOR of all elements of the subset. Example: Input: arr[] = {1, 2, 3, 4, 5}Output: 2Explanation: The subset {2, 3} has the bitwise AND of all elements as 2 while the bitwise XOR of
6 min read
Find maximum product of Bitwise AND and Bitwise OR of K-size subarray
Given an array arr[] containing N integers and an integer K, the task is to find the maximum value of the product of Bitwise AND and Bitwise OR of all elements of a K-sized subarray. Example: Input: arr[] = {1, 2, 3, 4}, K = 2Output: 6Explanation: Bitwise AND and Bitwise XOR of all K-sized subarrays is {(0, 3), (2, 3), (0, 7)} respectively. Therefo
9 min read
Find triplet A, B, C having bitwise AND of bitwise OR with each other as K
Given an integer K, the task is to find three distinct integers A, B and C such that ( A ∣ B ) & ( B ∣ C ) & ( C ∣ A ) = K, where | and & denotes bitwise OR and bitwise AND operation respectively. If there are multiple solutions, you may print any of them. Examples: Input: K = 3Output: 1 2 3Explanation: ( 1 ∣ 2 ) & ( 2 ∣ 3 ) & (
4 min read
Count of pairs with bitwise XOR value greater than its bitwise AND value | Set 2
Given an array arr that contains N positive Integers. Find the count of all possible pairs whose bit wise XOR value is greater than bit wise AND value Examples: Input : arr[]={ 12, 4 , 15}Output: 2Explanation: 12 ^ 4 = 8 , 12 & 4 = 4 . so 12 ^ 4 > 12 & 4 4 ^ 15 = 11, 4 & 15 = 4. so 4 ^ 15 > 4 & 15 . So , above two are valid pa
6 min read
Count of pairs with bitwise XOR value greater than its bitwise AND value
Given an array arr that contains N positive Integers. Find the count of all possible pairs whose bitwise XOR value is greater than bitwise AND value Examples: Input : arr[]={ 12, 4, 15}Output: 2Explanation: 12 ^ 4 = 8, 12 & 4 = 4. so 12 ^ 4 > 12 & 4 4 ^ 15 = 11, 4 & 15 = 4. so 4 ^ 15 > 4 & 15 . So, above two are valid pairs {
4 min read
Count pair of nodes with greater Bitwise AND than Bitwise XOR in given Linked List
Given a singly linked list, the task is to Count the pairs of nodes with greater Bitwise AND than Bitwise XOR. Examples: Input: list: 1->4->2->6->3Output: 2Explanation: 1st List Node Pair: (4, 6 ), Bitwise AND = 4, Bitwise XOR = 22nd List Node Pair: (2, 3), Bitwise AND = 2, Bitwise XOR = 1 Input: list: 17->34->62->46->30-
13 min read
Check if any Array pair has bitwise XOR greater than bitwise AND
Given an array arr[] of size N, the task is to find if there exists a pair in the array, such that their bitwise XOR is greater than their bitwise AND i.e. arr[i] ⊕ arr[j] > arr[i] & arr[j], (0 ≤ i < j ≤ N-1) where ⊕ represents the Bitwise XOR operator and & represents bitwise AND operator. Examples: Input: arr[] = {4, 5, 8, 6}Output:
9 min read
Generate original Array from the bitwise AND and Bitwise OR of adjacent elements
Given an integer N denoting the size of an array and two arrays containing Bitwise AND and Bitwise OR of adjacent elements of the array and the first element of the array X, the task is to build the original array. Examples: Input: N = 2, X(First element) = 2Bitwise OR = {3}, Bitwise AND = {2}Output: {2, 3} Input: N = 3, X(First element) = 3Bitwise
4 min read
Generate Array whose average and bitwise OR of bitwise XOR are equal
Given an integer N (N is odd). the task is to construct an array arr[] of size N where 1 ? arr[i] ? N such that the bitwise OR of the bitwise XOR of every consecutive pair should be equal to the average of the constructed arr[]. Formally: (arr[0] ^ arr[1]) | (arr[2] ^ arr[3] ) | (arr[4] ^ arr[5]) . . . (arr[N-3] ^ arr[N-2] ) | arr[N-1] = ( arr[0] +
4 min read
Find all possible pairs with given Bitwise OR and Bitwise XOR values
Given two positive integers A and B representing Bitwise XOR and Bitwise OR of two positive integers, the task is to find all possible pairs (x, y) such that x ^ y is equal to A and x | y is equal to B. Examples: Input: A = 5, B = 7Output:2 73 66 37 2Explanation:7( XOR )2 = 5 and 7( OR )2 = 73( XOR )6 = 5 and 3( OR )6 = 7 Input: A = 8, B = 10Output
8 min read
Article Tags :
Practice Tags :