Given two positive integers **n** and **m**. The problem is to check whether **n** is divisible by **2 ^{m}** or not without using arithmetic operators.

Examples:

Input : n = 8, m = 2 Output : Yes Input : n = 14, m = 3 Output : No

**Approach:** If a number is divisible by 2 then it has its least significant bit (LSB) set to 0, if divisible by 4 then two LSB’s set to 0, if by 8 then three LSB’s set to 0 and so on. Keeping this in mind, a number **n** is divisible by **2 ^{m}** if

**(n & ((1 << m) – 1))**is equal to 0 else not.

## C++

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

*chevron_right*

*filter_none*

## Java

`// JAVA Code for Check if n is divisible ` `// by power of 2 without using arithmetic ` `// operators ` `import` `java.util.*; ` ` ` `class` `GFG { ` ` ` ` ` `// function to chech whether n ` ` ` `// is divisible by pow(2, m) ` ` ` `static` `boolean` `isDivBy2PowerM(` `int` `n, ` ` ` `int` `m) ` ` ` `{ ` ` ` `// if expression results to 0, then ` ` ` `// n is divisible by pow(2, m) ` ` ` `if` `((n & ((` `1` `<< m) - ` `1` `)) == ` `0` `) ` ` ` `return` `true` `; ` ` ` ` ` `// n is not divisible ` ` ` `return` `false` `; ` ` ` `} ` ` ` ` ` `/* Driver program to test above function */` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `n = ` `8` `, m = ` `2` `; ` ` ` ` ` `if` `(isDivBy2PowerM(n, m)) ` ` ` `System.out.println(` `"Yes"` `); ` ` ` `else` ` ` `System.out.println(` `"No"` `); ` ` ` `} ` `} ` ` ` `// This code is contributed by Arnav Kr. Mandal. ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation to chech ` `# whether n is divisible by pow(2, m) ` ` ` `# function to chech whether n ` `# is divisible by pow(2, m) ` `def` `isDivBy2PowerM (n, m): ` ` ` ` ` `# if expression results to 0, then ` ` ` `# n is divisible by pow(2, m) ` ` ` `if` `(n & ((` `1` `<< m) ` `-` `1` `)) ` `=` `=` `0` `: ` ` ` `return` `True` ` ` ` ` `# n is not divisible ` ` ` `return` `False` ` ` `# Driver program to test above ` `n ` `=` `8` `m ` `=` `2` `if` `isDivBy2PowerM(n, m): ` ` ` `print` `(` `"Yes"` `) ` `else` `: ` ` ` `print` `( ` `"No"` `) ` ` ` `# This code is contributed by "Sharad_Bhardwaj". ` |

*chevron_right*

*filter_none*

## C#

`// C# Code for Check if n is divisible ` `// by power of 2 without using arithmetic ` `// operators ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `// function to chech whether n ` ` ` `// is divisible by pow(2, m) ` ` ` `static` `bool` `isDivBy2PowerM(` `int` `n, ` `int` `m) ` ` ` `{ ` ` ` `// if expression results to 0, then ` ` ` `// n is divisible by pow(2, m) ` ` ` `if` `((n & ((1 << m) - 1)) == 0) ` ` ` `return` `true` `; ` ` ` ` ` `// n is not divisible ` ` ` `return` `false` `; ` ` ` `} ` ` ` ` ` `/* Driver program to test above function */` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `n = 8, m = 2; ` ` ` ` ` `if` `(isDivBy2PowerM(n, m)) ` ` ` `Console.Write(` `"Yes"` `); ` ` ` `else` ` ` `Console.Write(` `"No"` `); ` ` ` `} ` `} ` ` ` `// This code is contributed by Sam007 ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP implementation to chech whether ` `// n is divisible by pow(2, m) ` ` ` `// function to chech whether n ` `// is divisible by pow(2, m) ` `function` `isDivBy2PowerM(` `$n` `, ` `$m` `) ` `{ ` ` ` ` ` `// if expression results to 0, then ` ` ` `// n is divisible by pow(2, m) ` ` ` `if` `((` `$n` `& ((1 << ` `$m` `) - 1)) == 0) ` ` ` `return` `true; ` ` ` ` ` `// n is not divisible ` ` ` `return` `false; ` `} ` ` ` ` ` `// Driver Code ` ` ` `$n` `= 8; ` ` ` `$m` `= 2; ` ` ` `if` `(isDivBy2PowerM(` `$n` `, ` `$m` `)) ` ` ` `echo` `"Yes"` `; ` ` ` `else` ` ` `echo` `"No"` `; ` ` ` `// This code is contributed by ajit ` `?> ` |

*chevron_right*

*filter_none*

Output:

Yes

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Smallest perfect power of 2 greater than n (without using arithmetic operators)
- Check if two numbers are equal without using arithmetic and comparison operators
- Add two numbers without using arithmetic operators
- Subtract two numbers without using arithmetic operators
- Subtract 1 without arithmetic operators
- Check if two numbers are equal without using comparison operators
- Check if a number is divisible by 17 using bitwise operators
- Check if a number is divisible by 8 using bitwise operators
- Check if given number is a power of d where d is a power of 2
- Operators in C | Set 2 (Relational and Logical Operators)
- Calculate 7n/8 without using division and multiplication operators
- Multiply a number by 15 without using * and / operators
- Find the sum of power of bit count raised to the power B
- Smallest of three integers without comparison operators
- Check if a number is multiple of 9 using bitwise operators
- Check if a number is positive, negative or zero using bit operators
- Check if a Number is Odd or Even using Bitwise Operators
- Efficiently check if a string has all unique characters without using any additional data structure
- Check if a number is power of 8 or not
- Check if bitwise AND of any subset is power of two

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.