Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Selected Reading
Check if n is divisible by power of 2 without using arithmetic operators in Python
Suppose we have two numbers x and n. We have to check whether x is divisible by 2^n or not without using arithmetic operators.
So, if the input is like x = 32 n = 5, then the output will be True as 32 = 2^5.
To solve this, we will follow these steps −
- if x AND (2^n - 1) is 0, then
- return True
- return False
Example
Let us see the following implementation to get better understanding −
def solve (x, n): if (x & ((1 << n) - 1)) == 0: return True return False x = 32 n = 5 print(solve(x, n))
Input
32, 5
Output
True
Advertisements
