Showing posts with label Pointer. Show all posts

Day 23: The Language of Machines | Binary & The Power Rule

📅 Date: Jan 23, 2026
🔥 Topic: Binary System & The 8-4-2-1 Rule


🤖 Why 0 and 1?

Humans use the Decimal System (Base 10) because we have 10 fingers. Computers run on electricity, which has only two states:

  • ON (1): Current is flowing.
  • OFF (0): Current is stopped.

⚡ The Power Rule (The Magic Formula)

I found a shortcut to read Binary numbers without complex math. It's called the Powers of 2 Rule. Instead of counting 1, 2, 3... we count by doubling the value from right to left.

128 64 32 16 8 4 2 1
2^7 2^6 2^5 2^4 2^3 2^2 2^1 2^0

🧠 How to find any Binary Number?

Let's say I want to write 13 in Binary.

Step 1: Look at the table above. Which numbers sum up to 13?
Answer: 8 + 4 + 1 = 13

Step 2: Put a '1' under those numbers and '0' elsewhere.

 8   4   2   1
[1] [1] [0] [1]  ->  1101

So, 13 in Binary is 1101.


💭 Thoughts

This "8-4-2-1" trick is super fast. I don't need to divide repeatedly; I just need to see which bits act like "switches" to make the total sum.

Day 22: Jumping Memory | Pointer Arithmetic

📅 Date: Jan 22, 2026
🔥 Topic: Pointer Arithmetic (+, -, ++, --)


🏃‍♂️ How Pointers Move

If I have a pointer at address 1000 and I do ptr++, where does it go?
It depends on the Type.

  • int (4 bytes): 1000 → 1004
  • char (1 byte): 1000 → 1001
  • double (8 bytes): 1000 → 1008

💻 Day 22 Code: Iterating without Index

#include <iostream>
using namespace std;

int main() {
    int arr[] = {10, 20, 30, 40};
    int *ptr = arr; // Points to start

    cout << "Traversing Array using Pointer:" << endl;

    for(int i=0; i<4; i++) {
        cout << *ptr << " ";
        ptr++; // Jumps 4 bytes ahead to next integer
    }

    return 0;
}

⚠️ Danger Zone

If I increment the pointer too much, it will point to memory that doesn't belong to me. This can crash the program (Segmentation Fault).

⏭️ Next Up: Binary & Number Systems

Pointers showed me memory addresses (Hexadecimal). Now it's time to understand the language of computers deeply.
Starting tomorrow: Binary to Decimal Conversions & Bitwise Operations!

Day 21: Arrays in Disguise | Pointers & Arrays

📅 Date: Jan 21, 2026
🔥 Topic: Relationship between Pointers & Arrays


🤯 The Big Reveal

Today I learned a shocking fact: The name of an array is actually a pointer to its first element.

If I have int arr[5], then just writing arr gives me the address of arr[0].

💻 Day 21 Code: Accessing Array via Pointer

I can access array elements without using square brackets []!

#include <iostream>
using namespace std;

int main() {
    int marks[] = {90, 80, 70};
    
    // Pointing to the first block
    int *p = marks;

    cout << "First element (*p): " << *p << endl;
    
    // Moving to next block logic (Internal)
    cout << "Second element *(p+1): " << *(p+1) << endl;
    
    // Formula: arr[i] == *(arr + i)
    cout << "Third element *(arr+2): " << *(marks+2) << endl;

    return 0;
}

💭 Thoughts

This explains why Arrays are 0-indexed. The index is just an "offset" from the starting address. *(arr + 0) is the start.

Day 20: The Memory Game | Introduction to Pointers

📅 Date: Jan 20, 2026
🔥 Topic: Pointers (Address & Dereference)


📍 What is a Pointer?

Normal variables store data (like 10, 'A', "Sahil").
Pointers are special variables that store the Address of another variable.

Think of it like a Treasure Map. The map isn't the gold, but it shows you exactly where the gold is kept.

🔑 Two Magic Operators

  • & (Address-of): Tells you where a variable lives.
  • * (Dereference): Goes to that address and grabs the value.

💻 Day 20 Code: Hacking Memory

#include <iostream>
using namespace std;

int main() {
    int num = 100;
    
    // 'ptr' is a pointer that holds the address of 'num'
    int *ptr = #

    cout << "Value of num: " << num << endl;
    cout << "Address of num (&num): " << &num << endl;
    
    cout << "Pointer stores: " << ptr << endl;
    cout << "Value at Pointer (*ptr): " << *ptr << endl;

    return 0;
}

💭 Thoughts

The syntax int *ptr declares a pointer, while *ptr (without int) accesses the value. Confusing at first, but powerful. I am literally touching the RAM addresses now.