Multiples of 4 (An Interesting Method)

31 May, 2022
Given a number n, the task is to check whether this number is a multiple of 4 or not without using +, -, * ,/ and % operators.Examples :  
Input: n = 4  Output - Yes
       n = 20 Output - Yes
       n = 19 Output - No
 
Method 1 (Using XOR) An interesting fact for n > 1 is, we do XOR of all numbers from 1 to n and if the result is equal to n, then n is a multiple of 4 else not.  
C++
Java
Python 3
C#
PHP
Javascript
// An interesting XOR based method to check if// a number is multiple of 4.#include<bits/stdc++.h>using namespace std; // Returns true if n is a multiple of 4.bool isMultipleOf4(int n){    if (n == 1)       return false;     // Find XOR of all numbers from 1 to n    int XOR = 0;    for (int i = 1; i <= n; i++)        XOR = XOR ^ i;     // If XOR is equal n, then return true    return (XOR == n);} // Driver code to print multiples of 4int main(){    // Printing multiples of 4 using above method    for (int n=0; n<=42; n++)       if (isMultipleOf4(n))         cout << n << " ";    return 0;}
// An interesting XOR based method to check if// a number is multiple of 4. class Test{    // Returns true if n is a multiple of 4.    static boolean isMultipleOf4(int n)    {        if (n == 1)           return false;              // Find XOR of all numbers from 1 to n        int XOR = 0;        for (int i = 1; i <= n; i++)            XOR = XOR ^ i;              // If XOR is equal n, then return true        return (XOR == n);    }         // Driver method    public static void main(String[] args)    {        // Printing multiples of 4 using above method        for (int n=0; n<=42; n++)           System.out.print(isMultipleOf4(n) ? n : " ");    }}
# An interesting XOR based# method to check if a# number is multiple of 4. # Returns true if n is a# multiple of 4.def isMultipleOf4(n):     if (n == 1):        return False     # Find XOR of all numbers    # from 1 to n    XOR = 0    for i in range(1, n + 1):        XOR = XOR ^ i     # If XOR is equal n, then    # return true    return (XOR == n) # Driver code to print# multiples of 4 Printing# multiples of 4 using# above methodfor n in range(0, 43):    if (isMultipleOf4(n)):        print(n, end = " ") # This code is contributed# by Smitha
// An interesting XOR based method// to check if a number is multiple// of 4.using System;class GFG {         // Returns true if n is a    // multiple of 4.    static bool isMultipleOf4(int n)    {        if (n == 1)        return false;             // Find XOR of all numbers        // from 1 to n        int XOR = 0;        for (int i = 1; i <= n; i++)            XOR = XOR ^ i;             // If XOR is equal n, then        // return true        return (XOR == n);    }         // Driver method    public static void Main()    {                 // Printing multiples of 4        // using above method        for (int n = 0; n <= 42; n++)        {            if (isMultipleOf4(n))                Console.Write(n+" ");        }    }} // This code is contributed by Smitha.
<?php// PHP program to check if// a number is multiple of 4. // Returns true if n is// a multiple of 4.function isMultipleOf4($n){    if ($n == 1)    return false;     // Find XOR of all    // numbers from 1 to n    $XOR = 0;    for ($i = 1; $i <= $n; $i++)        $XOR = $XOR ^ $i;     // If XOR is equal n,    // then return true    return ($XOR == $n);} // Driver Code // Printing multiples of 4// using above methodfor ($n = 0; $n <= 42; $n++)if (isMultipleOf4($n))    echo $n, " "; // This code is contributed by Ajit?>
<script>    // Javascript program of interesting XOR based method to check if// a number is multiple of 4.      // Returns true if n is a multiple of 4.    function isMultipleOf4(n)    {        if (n == 1)           return false;                // Find XOR of all numbers from 1 to n        let XOR = 0;        for (let i = 1; i <= n; i++)            XOR = XOR ^ i;                // If XOR is equal n, then return true        return (XOR == n);    } // Driver Code         // Printing multiples of 4 using above method        for (let n = 0; n <= 42; n++)           document.write(isMultipleOf4(n) ? n : " ");                     // This code is contributed by avijitmondal1998.</script>
Output :  
0 4 8 12 16 20 24 28 32 36 40 
Time Complexity: O(n)
Auxiliary Space: O(1)
How does this work? When we do XOR of numbers, we get 0 as XOR value just before a multiple of 4. This keeps repeating before every multiple of 4.  
Number Binary-Repr  XOR-from-1-to-n
1         1           [0001]
2        10           [0011]
3        11           [0000]
 Method 2 (Using Bitwise Shift Operators) The idea is to remove last two bits using >>, then multiply with 4 using <<. If final result is same as n, then last two bits were 0, hence number was a multiple of four.  
C++
Java
Python3
C#
PHP
Javascript
// An interesting XOR based method to check if// a number is multiple of 4.#include<bits/stdc++.h>using namespace std; // Returns true if n is a multiple of 4.bool isMultipleOf4(long long n){    if (n==0)        return true;     return (((n>>2)<<2) == n);} // Driver code to print multiples of 4int main(){    // Printing multiples of 4 using above method    for (int n=0; n<=42; n++)        if (isMultipleOf4(n))            cout << n << " ";    return 0;}
// An interesting XOR based method to check if// a number is multiple of 4. class Test{    // Returns true if n is a multiple of 4.    static boolean isMultipleOf4(long n)    {        if (n==0)            return true;              return (((n>>2)<<2) == n);    }         // Driver method    public static void main(String[] args)    {        // Printing multiples of 4 using above method        for (int n=0; n<=42; n++)           System.out.print(isMultipleOf4(n) ? n : " ");    }}
# Python3 code to implement an interesting XOR# based method to check if a number is multiple of 4. # Returns true if n is a multiple of 4.def isMultipleOf4(n):    if (n == 0):        return True     return (((n>>2)<<2) == n) # Driver code to print multiples of 4#Printing multiples of 4 using above methodfor n in range(43):    if isMultipleOf4(n):        print(n, end = " ") # This codeis contributed by phasing17
// An interesting XOR based method to// check if a number is multiple of 4.using System; class GFG {         // Returns true if n is a multiple    // of 4.    static bool isMultipleOf4(int n)    {        if (n == 0)            return true;             return (((n >> 2) << 2) == n);    }         // Driver code to print multiples    // of 4    static void Main()    {                 // Printing multiples of 4 using        // above method        for (int n = 0; n <= 42; n++)            if (isMultipleOf4(n))                Console.Write(n + " ");    }} // This code is contributed by Anuj_67
<?php// PHP program to check if// a number is multiple of 4. // Returns true if n is// a multiple of 4.function isMultipleOf4($n){    if ($n == 0)        return true;     return ((($n >> 2) << 2) == $n);} // Driver Code // Printing multiples of 4// using above methodfor ($n = 0; $n <= 42; $n++)    if (isMultipleOf4($n))        echo $n , " ";         // This code is contributed by anuj_67.?>
<script>     // An interesting XOR based method to    // check if a number is multiple of 4.         // Returns true if n is a multiple    // of 4.    function isMultipleOf4(n)    {        if (n == 0)            return true;              return (((n >> 2) << 2) == n);    }         // Printing multiples of 4 using    // above method    for (let n = 0; n <= 42; n++)      if (isMultipleOf4(n))        document.write(n + " ");         </script>
Output :  
0 4 8 12 16 20 24 28 32 36 40 
Time Complexity: O(1)
Auxiliary Space: O(1)
As we can see that the main idea to find multiplicity of 4 is to check the least two significant bits of the given number. We know that for any even number, the least significant bit is always ZERO (i.e. 0). Similarly, for any number which is multiple of 4 will have least two significant bits as ZERO. And with the same logic, for any number to be multiple of 8, least three significant bits will be ZERO. That’s why we can use AND operator (&) as well with other operand as 0x3 to find multiplicity of 4. This article is contributed by Sahil Chhabra(KILLER). If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@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
jit_t
Smitha Dinesh Semwal
avijitmondal1998
divyesh072019
surinderdawra388
subhammahato348
phasing17
Bitwise-XOR
Bit Magic
Bit Magic
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Set, Clear and Toggle a given bit of a number in C
Builtin functions of GCC compiler
Calculate XOR from 1 to n.
Find two numbers from their sum and XOR
Calculate square of a number without using *, / and pow()
Find a number X such that (X XOR A) is minimum and the count of set bits in X and B are equal
Reverse actual bits of the given number
Google Online Challenge 2020
Equal Sum and XOR of three Numbers
Unique element in an array where all elements occur k times except one