Bit Manipulation
Bit Manipulation
Learn why problem-solving skills are crucial in coding interviews and how
they contribute to better algorithmic thinking.
3 U n d e r s t a n d i n g Va r i o u s E r r o r s
Geeksforgeeks:
https://www.geeksforgeeks.org/
Codecademy: https://www.codecademy.com/
LeetCode: https://leetcode.com/
Hackerrank: https://www.hackerrank.com/
CodeChef: https://www.codechef.com/
Topcoder: https://www.topcoder.com/
GitHub: https://github.com/
Placement
• Academic curriculum
• Industry Demand
• Fill the Gap between Academic and
Industry
Dive into the world of bitwise operations and unravel their potential
applications in problem-solving.
1. AND (&):
• Takes two binary numbers and performs a bitwise AND operation
on each pair of corresponding bits.
2. OR (|):
• Performs a bitwise OR operation on each pair of corresponding
bits in two binary numbers.
3. XOR (^):
• Performs a bitwise XOR (exclusive OR) operation on each pair of
corresponding bits.
4. NOT (~):
• Inverts each bit of a binary number, changing 0s to 1s and 1s to 0s.
5. Left Shift (<<) and Right Shift (>>):
• Shifts the bits of a binary number to the left or right by a specified
number of positions.
These bitwise operations are often used in programming for tasks such as
setting or clearing specific bits, checking the parity of numbers, or
optimizing certain algorithms.
Bitwise Real Case Applications:
Bitwise Flags:
It is commonly used to represent sets of Boolean flags. Each flag corresponds to a specific bit,
and bitwise operations can be used to set, clear, or toggle individual flags.
# Define flags
READ = 1 # 0001
WRITE = 2 # 0010
EXECUTE = 4 # 0100
# Set flags
permissions = 0 # 0000
permissions |= READ | WRITE
print(bin(permissions))
Optimizations:
Bitwise operations are sometimes used for low-level optimizations, particularly in embedded
systems or performance-critical applications. For example, bitwise AND with a power of two is
equivalent to taking the modulus by that power, which can be more efficient in certain scenarios.
Graphics Programming:
In graphics programming, bitwise operations can be employed for tasks such as pixel
manipulation, color blending, and image processing.
File Compression:
Bitwise operations are utilized in file compression algorithms, where individual bits
are manipulated to represent patterns and compress data.
Embedded Systems:
In embedded systems programming, where resources are often limited, bitwise
operations can be used to efficiently control hardware registers and manage
memory.
Examples of Bit Manipulation in
Coding Challenges
}
Bitwise Programming Tips and Tricks:
}
Bitwise Programming Tips and Tricks:
}
Bitwise Programming Tips and Tricks:
}
Bitwise Programming Tips and Tricks:
}
Bitwise Programming Tips and Tricks:
}
Bitwise Programming Tips and Tricks:
}
Bitwise Programming Tips and Tricks:
Approach?
}
Hands-on Practice
Question: Write a function to add two numbers without using arithmetic
+ or – operator.
Approach?
ARR: 5, 4, 2, 5, 4
Output: 2
Approach?
function singleNumber(nums)
{
}
Hands-on Practice
Question: Let a non-empty array of integers ARR is given where every element
appears twice except for one.
Find that single one.
ARR: 5, 4, 2, 5, 4
Output: 2
Approach?
function singleNumber(nums) {
let answer = 0;
for (let i = 0; i < nums.length; i++) {
answer ^= nums[i];
}
return answer;
}
Hands-on Practice
Given a number N, the task is to find the XOR of all numbers from 1
to N.
Examples :
Input : N = 6
Output : 7
// 1 ^ 2 ^ 3 ^ 4 ^ 5 ^ 6 = 7
Input : N = 7
Output : 0
// 1 ^ 2 ^ 3 ^ 4 ^ 5 ^ 6 ^ 7 = 0
Solution:
Find the remainder of N by moduling it with 4.
If rem = 0, then xor will be same as N.
If rem = 1, then xor will be 1.
If rem = 2, then xor will be N+1.
If rem = 3 ,then xor will be 0
U n d e r s t a n d i n g Va r i o u s E rro r s
1. Syntax Errors - Syntax errors can prevent the code from compiling
4. Time Limit Exceeded (TLE) - Exceeding the allowed execution time leads to
rejection of the solution, even if it produces correct results.
5. Memory Limit Exceeded (MLE) - If the program uses more memory than
allowed, it can result in an MLE error.