Bit Manipulation Technique
1. Introduction to Bit and Binary Numbers
A bit is the smallest unit of data in a computer and can have a value of either 0 or 1. A sequence of bits can represent numbers, with binary being the base-2 numeral system used by computers. For example, the binary number 101 represents the decimal number 5.
In bit manipulation, we deal with data at the bit level. Operations such as setting, clearing, flipping, or shifting bits are performed using bitwise operators.
NOTE: In programming, when you read a series of bits (binary sequence), you start from the right. The last bit on the right is called the Least Significant Bit (LSB), and the first bit on the left is the Most Significant Bit (MSB).
Video Explanation

2. Basics of bit manipulation:
a. AND (&)
The AND operator compares each bit of two numbers and returns 1 if both bits are 1, otherwise 0.
5 & 3 = 101 & 011 = 001 = 1
b. OR (|)
The OR operator compares each bit of two numbers and returns 1 if at least one of the bits is 1, otherwise 0.
5 | 3 = 101 | 011 = 111 = 7
c. XOR (^)
The XOR operator returns 1 if the corresponding bits of the two numbers are different, otherwise it returns 0.
5 ^ 3 = 101 ^ 011 = 110 = 6
d. NOT (~)
The NOT operator inverts the bits of the number (i.e., it converts 1 to 0 and 0 to 1).
~5 = ~101 = ...11111010 (depends on bit width, typically 32 bits in practice)
e. Left Shift
The left shift operator shifts the bits of the number to the left by a specified number of positions. For each shift, the leftmost bits are discarded, and zeros are filled in on the right.
5 << 1 = 101 << 1 = 1010 = 10
f. Right Shift
The right shift operator shifts the bits of the number to the right by a specified number of positions. For each shift, the rightmost bits are discarded.
5 >> 1 = 101 >> 1 = 10 = 2