Problem Statement:
You are given a number, you need to check if the number is divisible by 8 using bitwise operators.
Example:
Input : 16
Output :Yes
Solution Explanation:
Solution is very simple.
Shift 3 bit to right then shift 3 bit to left and then check if the number is same as the original number, then it is divisible by 8.
Time Complexity: O(1)
Space Complexity: O(1)
Code Solution
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int solution_1(int n)
{
return (((n >> 3) << 3) == n);
}
int main()
{
int n = 16;
if (solution_1(n))
cout << "Yes" << endl;
else
cout << "No";
return 0;
}
Output
Yes