Day 53
Day 53: Bit Manipulation Techniques
53/60 Days
Day 53: Bit Manipulation Techniques #
Welcome to Day 53 of our 60 Days of Coding Algorithm Challenge! Today, we’ll explore Bit Manipulation Techniques, a set of powerful tools that allow us to perform operations at the bit level, often leading to more efficient algorithms.
What is Bit Manipulation? #
Bit manipulation involves the use of bitwise operators to manipulate individual bits within a data type. These operations are often used to optimize performance, reduce memory usage, or solve certain types of problems more efficiently.
Basic Bitwise Operators #
- AND (&): Returns 1 if both bits are 1, otherwise 0.
- OR (|): Returns 1 if at least one bit is 1, otherwise 0.
- XOR (^): Returns 1 if the bits are different, otherwise 0.
- NOT (~): Inverts all the bits.
- Left Shift («): Shifts bits to the left, filling with 0.
- Right Shift (»): Shifts bits to the right, filling with the sign bit (arithmetic) or 0 (logical).
Common Bit Manipulation Techniques #
1. Check if a number is even or odd #
1def is_even(n):
2 return not (n & 1)
3
4# Example usage
5print(is_even(4)) # True
6print(is_even(7)) # False
2. Check if the i-th bit …
Continue Reading
Sign up or log in to access the full lesson and all 60 days of algorithm content.