The basics of bit manipulation
1 min readJan 14, 2023
Unlocking the Power of Bit Manipulation: Techniques and Best Practices
- Bit manipulation is the process of manipulating individual bits of a binary number.
- It is often used in computer programming for optimization or low-level operations.
- The basic operations in bit manipulation include bitwise AND, OR, XOR, NOT, left shift, and right shift.
- Bitwise AND (&) sets a bit to 1 if both input bits are 1, otherwise it sets the output bit to 0.
- Bitwise OR (|) sets a bit to 1 if either input bit is 1, otherwise it sets the output bit to 0.
- Bitwise XOR (^) sets a bit to 1 if the input bits are different, otherwise it sets the output bit to 0.
- Bitwise NOT (~) inverts all the bits of the input number.
- Left shift (<<) moves all bits in a number to the left by a specified number of places, adding 0s to the right.
- Right shift (>>) moves all bits in a number to the right by a specified number of places, adding 0s or removing bits on the left.
- It is important to keep in mind the size of the data type and the overflow/underflow that may occur during bit manipulation.