Pages

Tuesday, May 6, 2025

Classes & Objects in OOPs

in Leetcode Playground: 


class Student {

    //Properties

    string name;

    float cgpa;

    

    //Methods

    void getPercentage() {

        cout << (cgpa*10) << endl;

    }

};


class User{

    int id;

    string username;

    string password;

    string bio; 

    

    void deactivate() {

        cout << "deactivating account\n";

    }

    

    void editBio(string newBio) {

        bio = newBio;

    }

}


int main() {

    Student s1; // objects

    cout << sizeof(s1) << endl;

    

    Student s2; // objects

    cout << sizeof(s2) << endl;

}

Saturday, May 3, 2025

Q. Clear range from ith to jth bit (Bit Manipulation)

 #include <iostream>

using namespace std;

// goal- to clear from left and right bit of number

int main()

{

    int i = 1, j = 3;

    int num = 31; // 0b01111


    int left = ~0 << (j + 1);       // Clears bits j and above

    int right = (1 << i) - 1;       // Sets bits below i

    int bitMask = left | right;     // Clears bits from i to j

    int result = num & bitMask;     // Apply the mask


    cout << "Bitmask: " << bitMask << endl;

    cout << "Result: " << result << endl;


    return 0;

}


Friday, May 2, 2025

WAF to clear ith bits of a number. in C++ (Bit Manipulation)

 #include <iostream>

using namespace std;


void clearIthBits(int num, int i) {

    int bitMask = (~0) << i;

    num = num & bitMask;

    

    cout << num << endl;

}


int main()

{


    clearIthBits(15, 2);

    

    return 0;

}

Update Ith Bit in Bit Manipulation in C++

 #include <iostream>

using namespace std;


void updateIthBit(int num, int i, int val) {

    // clear ith bit 

    num = num & ~(1<<i);

    

    //numbitmask

    num = num | (val << i);

    

    // print 

    cout << num << endl;

}


int main()

{

    updateIthBit(7, 3, 1);


    return 0;

}

Check for power of 2 in Bit Manipulation in C++

 #include <iostream>

using namespace std;


bool isPowerOf2(int num) {

    if (!(num & (num-1))) { // if not equal to num & num-1

        return true;

    } else {

        return false;

    }

}



int main()

{

    cout << isPowerOf2(4) << endl;

    cout << isPowerOf2(16) << endl;

    cout << isPowerOf2(13) << endl;

    cout << isPowerOf2(7) << endl;


    return 0;

}

Clear ith bit - Bit Manipulation in C++

 #include <iostream>

using namespace std;


int setIthBit(int num, int i) {

    int bitMask = 1 << i;

    return (num | bitMask);

}


int clearIthBit(int num, int i) {

    int bitMask = ~(1 << i);

    return num & bitMask;

}


int main()

{

    cout<< clearIthBit(6, 1) << endl;


    return 0;A

}

Thursday, May 1, 2025

Set ith Bit in Bitwise Operator in C++

 #include <iostream>

using namespace std;


int setIthBit(int num, int i)  {

    int bitMask = 1 << i;

    

    return num | bitMask;

    // if(num | 1 << i)   -> mistake ?

}


int main()

{

    cout<< setIthBit(6, 3); 


    return 0;

}

how to become red on codeforces by Errichto Algorithms | explained simply in a blog

 Yes. I found an available transcript of the video and summarized it simply. The video is “How To Become Red Coder? (codeforces.com)” by Err...