![]() |
VOOZH | about |
Bit manipulation is a technique in competitive programming that involves the manipulation of individual bits in binary representations of numbers. It is a valuable technique in competitive programming because it allows you to solve problems efficiently, often reducing time complexity and memory usage.
Table of Content
Bitwise Operators are used to perform operations on individual bits in binary representations of numbers. Some common bitwise operators that are used in competitive programming:-
This can be done by left-shifting the value 1 by 'pos' positions (1<< pos) and performing a bitwise OR operation with number n. This operation effectively turns on the bit at the specified position.
This can be done by left-shifting the value 1 by pos positions (1<< pos) and then use bitwise NOT operator ‘~’ to unset this shifted 1, making the bit at position pos to 0 and then use Bitwise AND with the number n that will unset bit at desired positoon of number n.
Use the bitwise XOR (^) operator to toggle (flip) the bit at the given position. If the bit is 0, it becomes 1, and if it's 1, it becomes 0.
This can be done by performing a bitwise AND operation with a mask having only that bit set. If the result is non-zero, the bit is set; otherwise, it's unset.
A power of two is a number with only one bit set in its binary representation, while the number just before it has that bit unset and all the following bits set. Consequently, when you perform a bitwise AND operation between a number and its predecessor, the result will always be 0.
Suppose you are given an array a of n numbers and q queries and each query is of the form (l,r). The task is to compute Bitwise AND of the numbers from index l to r i.e., (al & al+1 ..... & ar-1 & ar).
A simple approach will be for each query travese from index l to r and compute Bitwise AND. By this we will be able to answer each query in O(n) time in worst case.
But to answer each query in constant time prefix sum can be a useful method.
Below is the code for above approach:
Bitwise AND of range [2,4] is: 2
Note- When you increase the range for Bitwise AND, the result will never increase; it will either stay the same or decrease. This is a useful property and we can apply Binary search on answer we are given to determine the largest range whose Bitwise AND is greater than or equal to a given number.
Bitwise OR can be computed in a similar way. WE will make temp and psum array in a similar way,
Below is the code for above approach:
Bitwise OR of range [2,4] is: 11
Note: When you increase the range for Bitwise OR, the result will never decrease; it will either stay the same or increase. Again this is a useful property and we can apply Binary search on answer we are given to determine the smallest range whose Bitwise OR is smaller than or equal to a given number.
Bitwise XOR for a range can be done in similar way:
👁 Bit-Manipulation-for-Competitive-Programming-2
Below is the implementation of the above approach:
Bitwise XOR of range [2,4] is :10
a|b = a⊕b + a&b
a⊕(a&b) = (a|b)⊕b
(a&b)⊕(a|b) = a⊕b
a+b = a|b + a&b
a+b = a⊕b + 2(a&b)
In most of the problems involving bit manipulation it is better to work bit by bit i.e., break down the problem into individual bits. Focus on solving the problem for a single bit position before moving on to the next.
Let's consider few examples:
Given an integer array arr. The task is to find the size of largest subset such that bitwise AND of all the elements of the subset is greater than 0.
- To start, notice that for a subset's bitwise AND to be greater than zero, there must be a bit position where all the elements in the subset have that bit set to 1.
- We approach this problem bit by bit, examining each of the 32 possible bit positions in the numbers.
- For each bit position, we count how many elements in the array have that bit set to 1.
- Our answer is the largest count of elements that have their bit set for a particular bit position.
Given an integer array arr of size n. A graph is formed using these elements. There exists an edge between index i and index j if i!=j and a[i] AND a[j]>0. The task is to determine whether there exists a cycle in the graph.
We begin by analyzing each bit position in the binary representation of the numbers and for each bit determine how many elements have that bit set.
- For a specific bit position,
- If there are more than two elements in the array that have that bit set, it indicates that there exists a cycle in the graph.
- Otherwise, there will be almost 2 numbers that have a particular bit set. It follows that each bit can contribute to atmost 1 edge.
- Importantly, the entire graph won't have more than 32 edges because each number in the array is represented using 32 bits.
- To ascertain the presence of a cycle in the graph,a straightforward Depth-First Search (DFS) algorithm can be used.
Shortest path length between two given nodes such that adjacent nodes are at bit difference 2 |
|---|
Calculate Bitwise OR of two integers from their given Bitwise AND and Bitwise XOR values |