Grokking Bit Manipulation For Coding Interviews
Bit Manipulation is a powerful technique that solves bit-related problems mostly in constant time.
Table of Contents
Course Overview
In this course, you will learn how to solve problems using bit manipulation, a powerful technique that can be used to optimize your algorithmic and problem-solving skills.
This is one of the most important/critical topics when someone starts preparing for coding interviews for FAANG companies.
To kick things off, you’ll start by learning about the number system and how it’s represented. Then you’ll learn about the six bitwise operators: AND, OR, NOT, XOR, and bit shifting. Throughout, you will get tons of hands-on experience working through practice problems to help sharpen your understanding. By completing this course, you can solve problems faster and more efficiently.
What Do You Get?
🧸 45 lessons were categorized based on topics.
🧸 28 challenges.
🧸 30+ Illustrations and sketches
🧸 271 solutions provided.
🧸 Solutions are written in Java, Python, JavaScript, C++, and TypeScript.
🧸 GitHub repository access - Java, JavaScript
Takeaway Skills
- Master problem-solving that involves bit manipulation.
- Master the bit manipulation allows you to organize all inputs in binary representation at the memory levels.
- Master how the bit-level operations are computed. Understand that bit-level operations are based on all the arithmetic operations built into all languages.
- Solve problems that are commonly asked in coding interviews related to bit manipulation.
- These bit tricks could help in competitive programming in running algorithms, mostly in
O(1)
time.
Estimated Time: 8hrs to complete.
Getting Started
Introductory learning topics about bit-manipulation. Start here to learn the basics.
- Introduction to bit manipulation (preview)
- What is bit manipulation? (preview)
Gift
Number Systems, Bitwise, and Binary
Next, you need to understand the number systems in mathematics like binary, decimal, octal, and hexadecimal. You will be introduced to number representations and mathematical conversions.
- Introduction to number systems (preview)
- Decimal number system (preview)
- Binary number system and representation (preview)
- What are bitwise operators? (preview)
- Count the number of digits in an integer (preview)
- Convert Decimal Numbers to binary numbers (preview)
Bitwise AND
The Bitwise AND operator is denoted by &
. When an AND gate is given with 2 inputs, the corresponding outputs will be: If two input bits are 1
, then the output is 1
. In all other cases, it is 0
.
- Introduction to AND operator
- Bitwise AND, computations, and examples
- Challenge 1: Count set bits or number of 1 bit's (preview)
- Solution Review: Count set bits or number of 1 bit's (preview)
- Counting bits II
- Challenge 2: Check if a given number is even/odd
- Solution Review: Check if a given number is even/odd
- Challenge 3: Power of 2 (preview)
- Solution Review: Power of 2 (preview)
Bitwise OR
The Bitwise OR operator is denoted by |
. When an OR gate is given with 2 inputs, the corresponding outputs will be: If two input bits are 0
, then the output is 0
. In all other cases, it is 1
.
- Introduction to OR operator
- Bitwise OR, computations, and examples
- Number of flips required to make a|b equals c
Bitwise NOT
The Bitwise NOT operator evaluates the binary representation of the value of a single input. If the bit contains 1
, the output will be 0
for that bit location. If the bit contains 0
, the output will be 1
.
Bitwise XOR
The Bitwise XOR operator is denoted by ^
. When an XOR gate is given with 2 inputs, the corresponding outputs will be: If two input bits are different, the output is 1
. In all other cases, it is 0
.
- Introduction to XOR
- Bitwise XOR, computations, and examples
- Swap two numbers
- Find odd occurring element
- Detect if two integers have opposite signs
- Hamming distance
- Challenge 1: Single number
- Solution Review: Single number
- Challenge 2: Missing number
- Solution Review: Missing number
Bit Shifring - Left, Right
A bit shift is a Bitwise operation where the order of a series of bits is moved, either to the left or right, to efficiently perform a mathematical operation.
Bitwise Left Shift Problems
Problems on left shift.
- Find the bit length of a number
- Check if the kth bit is set/unset using left shift
- Subsets or Powerset (preview)
- Challenge 1: Get the first set bit position using the left shift
- Solution Review: Get the first set bit position using the left shift
Bitwise Right Shift Problems
Problems on the right shift.
- Check if the kth bit is set/unset using right shift
- Challenge 1: Get the first set bit position using the right shift
- Solution Review: Get the first set bit position using the right shift
Final Thoughts
This concludes the course on bit manipulation.
This course has taught you how to re-write arithmetic operators like +, -, /, *
into left, right shift, and other bit-level operations.
I hope the learning you gained in this course will be useful in solving Leetcode, Hackerrank, HackerEarth, Codeforces, Codechef, and other online coding challenges. This will also help you with interviews at FAANG or other top tech companies.
I wish you good luck and hope this course makes a difference in your coding journey!
Note: In the upcoming edits, you will see the following items:
- Solutions in Rust and Go.
- A few medium and hard problems were asked in recent FAANG and fast-paced startup coding interviews.
Happy Coding 👨🏻💻 !!
If you have finished the course, I'm super happy for you 🥳, and it's been a pleasure to be part of your professional journey.
I'm working hard in my spare time to keep the site up and running and to update the site with the most requested features. So long as people are having fun, I want to keep the site going.
👨🏻💻 Gopi Gorantala Newsletter
Join the newsletter to receive the latest updates in your inbox.