Input: n = 13 Output: true Explanation: n = 13 = 1101 As the last bit is set bit so return true. Input: n = 8 Output: false Explanation: n = 8 = 1000 As the last bit is not set bit so return false.
Approach.
// Example C++: Check if last bit is Set bit
#include <iostream>
using namespace std;
void checkLastBit(int n) {
if (n & 1)
cout << "Last bit is set (1)" << endl;
else
cout << "Last bit is not set (0)" << endl;
}
int main() {
int n;
cout << "Enter a number: ";
cin >> n;
checkLastBit(n);
return 0;
}
Enter a number: 13
Last bit is set (1)
Trends is an amazing magazine Blogger theme that is easy to customize and change to fit your needs.