๐Ÿ“… Date: Feb 1, 2026
๐Ÿ”ฅ Topic: Bitwise Operators, Shifts, Odd/Even Check


๐ŸŽ‰ Month 2 Begins!

Welcome to the second leg of this journey. We are going deep into the machine level today. Bit Manipulation is the art of modifying bits (0s and 1s) directly. It's fast, efficient, and makes you feel like a true hacker.

๐Ÿ› ️ The Bitwise Operators

Just like we have `+` and `-` for numbers, we have operators for bits.

Operator Name Logic
& AND Both 1 → 1. Else 0.
| OR Any 1 → 1. Both 0 → 0.
^ XOR Different → 1. Same → 0.
~ One's Complement Flips all bits (0→1, 1→0).
<< Left Shift Multiplies by 2 (a << b).
>> Right Shift Divides by 2 (a >> b).

⚡ Trick 1: Odd or Even (The Fast Way)

Normally we use n % 2 == 0. But in binary:
Odd numbers always end with 1.
Even numbers always end with 0.

So, n & 1 will give 1 if Odd and 0 if Even. This is much faster than division!

๐Ÿ” Trick 2: Get ith Bit

How do I check if the 3rd bit of a number is 0 or 1?
Logic: Shift the number to the right by `i` times, then AND with 1.
(n >> i) & 1


๐Ÿ’ป Day 31 Code: Basic Operations

#include <iostream>
using namespace std;

int main() {
    int n = 5; // Binary: 0101
    int i = 2; // Check 2nd bit (0-indexed)

    cout << "--- Bitwise Basics ---" << endl;
    
    // 1. Odd/Even Check
    if (n & 1) 
        cout << n << " is Odd" << endl;
    else 
        cout << n << " is Even" << endl;

    // 2. Left Shift (Multiply by 2)
    cout << "5 << 1 = " << (n << 1) << " (becomes 10)" << endl;

    // 3. Get ith Bit
    int bitStatus = (n >> i) & 1;
    cout << "The " << i << "th bit of " << n << " is: " << bitStatus << endl;

    return 0;
}

๐Ÿ’ญ Thoughts

Bitwise operators feel like manipulating the DNA of the number. The Left Shift << is a super cool way to multiply without actually multiplying.