Efficiently check whether n is a multiple of 4 or not - GeeksforGeeks

28 Apr, 2021
Given a number n. The problem is to efficiently check whether n is a multiple of 4 or not without using arithmetic operators. Examples:  
Input : 16
Output : Yes

Input : 14
Output : No
 
Approach: A multiple of 4 always has 00 as its last two digits in its binary representation. We have to check whether the last two digits of n are unset or not.How to check whether the last two bits are unset or not. If n & 3 == 0, then the last two bits are unset, else either both or one of them are set.  
C++
Java
Python 3
C#
PHP
Javascript
// C++ implementation to efficiently check whether n// is a multiple of 4 or not#include <bits/stdc++.h>using namespace std; // function to check whether 'n' is// a multiple of 4 or notstring isAMultipleOf4(int n){       // if true, then 'n' is a multiple of 4    if ((n & 3) == 0)        return "Yes";     // else 'n' is not a multiple of 4    return "No";} // Driver program to test aboveint main(){    int n = 16;    cout << isAMultipleOf4(n);    return 0;}
// Java implementation to efficiently check// whether n is a multiple of 4 or not class GFG {    // method to check whether 'n' is    // a multiple of 4 or not    static boolean isAMultipleOf4(int n)    {        // if true, then 'n' is a multiple of 4        if ((n & 3) == 0)            return true;         // else 'n' is not a multiple of 4        return false;    }     // Driver method    public static void main(String[] args)    {        int n = 16;        System.out.println(isAMultipleOf4(n) ? "Yes" : "No");    }}
# Python 3 implementation to# efficiently check whether n# is a multiple of 4 or not # function to check whether 'n'# is a multiple of 4 or notdef isAMultipleOf4(n):     # if true, then 'n' is    # a multiple of 4    if ((n & 3) == 0):        return "Yes"     # else 'n' is not a    # multiple of 4    return "No" # Driver Codeif __name__ == "__main__":     n = 16    print (isAMultipleOf4(n)) # This code is contributed# by ChitraNayal
// C# implementation to efficiently check// whether n is a multiple of 4 or notusing System; class GFG {         // method to check whether 'n' is    // a multiple of 4 or not    static bool isAMultipleOf4(int n)    {        // if true, then 'n' is a multiple of 4        if ((n & 3) == 0)            return true;         // else 'n' is not a multiple of 4        return false;    }     // Driver method    public static void Main()    {        int n = 16;        Console.WriteLine(isAMultipleOf4(n) ? "Yes" : "No");    }} // This code is contributed by vt_m.
<?php// PHP implementation to// efficiently check whether n// is a multiple of 4 or not // function to check whether 'n' is// a multiple of 4 or notfunction isAMultipleOf4($n){         // if true, then 'n'    // is a multiple of 4    if (($n & 3) == 0)        return "Yes";     // else 'n' is not    // a multiple of 4    return "No";}     // Driver Code    $n = 16;    echo isAMultipleOf4($n);     // This code is contributed by anuj_67.?>
<script> // Javascript implementation to efficiently check// whether n is a multiple of 4 or not         // method to check whether 'n' is    // a multiple of 4 or not    function isAMultipleOf4(n)    {        // if true, then 'n' is a multiple of 4        if ((n & 3) == 0)            return true;           // else 'n' is not a multiple of 4        return false;    }         // Driver method    let n = 16;    document.write(isAMultipleOf4(n) ? "Yes" : "No");         //This code is contributed by rag2127     </script>
Output:  
Yes
Can we generalize above solution? Similarly we can check for other powers of 2. For example, a number n would be multiple of 8 if n & 7 is 0. In general we can say.  
// x must be a power of 2 for below
// logic to work
if (n & (x -1) == 0)
   n is a multiple of x
Else
   n is NOT a multiple of x
 
https://youtu.be/ke
-Mai2m6Cg This article is contributed by Ayush Jauhri. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
vt_m
ukasp
shubhamjaiswal16
rag2127
divisibility
Bit Magic
Bit Magic
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Cyclic Redundancy Check and Modulo-2 Division
Little and Big Endian Mystery
Binary representation of a given number
Add two numbers without using arithmetic operators
Josephus problem | Set 1 (A O(n) Solution)
Find the element that appears once
Bits manipulation (Important tactics)
Set, Clear and Toggle a given bit of a number in C
Bit Fields in C
C++ bitset and its application