Toggle the bit at Kth position of integer n
#include <iostream>
using namespace std;
//function to toggle Kth bit of integer n
unsigned toggleBit(unsigned int n, unsigned int k)
{
int result = n ^ (1 <<(k - 1));
return result;
}
int main()
{
unsigned int n = 10, k = 1;
unsigned int result = toggleBit(n, k);
cout << "After toggling bit of position " << k << " integer "<< n << ", we get "<< result<<"." << endl;
return 0;
}
Comments
Post a Comment