Program to set Nth bit of a number in C++
#include <iostream>
using namespace std;
int main()
{
int num, n;
cout << "Enter number: ";
cin >> num;
cout << "Enter bit number you wish to set: ";
cin >> n;
cout << "Bit set Successfully" << endl;
cout << "Answer: " << (num | (1 << (n - 1))) << endl;
return 0;
}
Output
Enter Number
13
Enter bit number you wish to set
2
Bit set Successfully
Answer: 15
