Check if n is divisible by power of 2 without using arithmetic operators - GeeksforGeeks

16 Aug, 2021
Given two positive integers n and m. The problem is to check whether n is divisible by 2m 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 2m if (n & ((1 << m) – 1)) is equal to 0 else not. 
C++
Java
Python3
C#
PHP
Javascript
// C++ implementation to check whether n// is divisible by pow(2, m)#include <bits/stdc++.h> using namespace std; // function to check 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 aboveint main(){    unsigned int n = 8, m = 2;    if (isDivBy2PowerM(n, m))        cout << "Yes";    else        cout << "No";    return 0;}
// JAVA Code for Check if n is divisible // by power of 2 without using arithmetic// operatorsimport java.util.*; class GFG {         // function to check 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.
# Python3 implementation to check# whether n is divisible by pow(2, m) # function to check 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 aboven = 8m = 2if isDivBy2PowerM(n, m):    print("Yes")else:    print( "No")     # This code is contributed by "Sharad_Bhardwaj".
// C# Code for Check if n is divisible// by power of 2 without using arithmetic// operatorsusing System; class GFG {     // function to check 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
<?php// PHP implementation to check whether// n is divisible by pow(2, m) // function to check 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?>
<script> // JavaScript implementation to check whether n// is divisible by pow(2, m) // function to check 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 program to test above     let n = 8, m = 2;    if (isDivBy2PowerM(n, m))        document.write("Yes");    else        document.write("No"); // This code is contributed by Surbhi Tyagi. </script>
Output: 
Yes
YouTubeGeeksforGeeks501K subscribersCheck if n is divisible by power of 2 without using arithmetic operators | GeeksforGeeksWatch laterShareCopy linkInfoShoppingTap to unmuteIf playback doesn't begin shortly, try restarting your device.You're signed outVideos you watch may be added to the TV's watch history and influence TV recommendations. To avoid this, cancel and sign in to YouTube on your computer.CancelConfirmMore videosMore videosSwitch cameraShareInclude playlistAn error occurred while retrieving sharing information. Please try again later.Watch on0:000:000:00 / 2:41•Live•<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=4UgLrh_foDE" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>
 
jit_t
surbhityagi15
akshaysingh98088
Bit Magic
Bit Magic
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Set, Clear and Toggle a given bit of a number in C
Program to find parity
Hamming code Implementation in C/C++
Check whether K-th bit is set or not
Write an Efficient Method to Check if a Number is Multiple of 3
Implementation of Bit Stuffing and Bit Destuffing
Builtin functions of GCC compiler
Count total bits in a number
Swap bits in a given number
Check for Integer Overflow