Clear the bit at Kth position of integer n

 

#include <iostream> 
using namespace std;

//function to clear Kth bit of integer n
unsigned clearBit(unsigned int n, unsigned int k)
{
int result = n & ~(1 <<(k - 1));
return result;
}

int main()
{
unsigned int n = 10, k = 2;
unsigned int result = clearBit(n, k);
cout << "After clearing bit of position " << k << " integer "<< n << ", we get "<< result<<"." << endl;
return 0;
}

Comments

Popular posts from this blog

Clear right most set bit of an integer n

Swap bits at ith and jth position of integer n

Toggle the bit at Kth position of integer n