Check if n is divisible by power of 2 without using arithmetic operators | C ++ implementation to check whether n is divisible by pow ( 2 , m ) ; function to check whether n is divisible by pow ( 2 , m ) ; if expression results to 0 , then n is divisible by pow ( 2 , m ) ; n is not divisible ; Driver program to test above
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isDivBy2PowerM ( unsigned int n , unsigned int m ) { if ( ( n & ( ( 1 << m ) - 1 ) ) == 0 ) return true ; return false ; } int main ( ) { unsigned int n = 8 , m = 2 ; if ( isDivBy2PowerM ( n , m ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }