Pages

Friday, May 2, 2025

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;

}

No comments:

Post a Comment

Stack using Linked List – Partial (University Exam Topic)

 #include <iostream> using namespace std; struct Node {     int data;     Node* next; }; class Stack {     Node* top; public:     Stac...