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/C++

`// 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 not ` `string 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 above ` `int` `main() ` `{ ` ` ` `int` `n = 16; ` ` ` `cout << isAMultipleOf4(n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// 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"` `); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python 3

`# 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 not ` `def` `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 ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `n ` `=` `16` ` ` `print` `(isAMultipleOf4(n)) ` ` ` `# This code is contributed ` `# by ChitraNayal ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation to efficiently check ` `// whether n is a multiple of 4 or not ` `using` `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. ` |

*chevron_right*

*filter_none*

## PHP

`<?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 not ` `function` `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. ` `?> ` |

*chevron_right*

*filter_none*

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

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.

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:

- Efficiently check if a string has all unique characters without using any additional data structure
- Check whether a very large number of the given form is a multiple of 3.
- Efficiently find first repeated character in a string without using any additional data structure in one traversal
- Finding the Parity of a number Efficiently
- Program to invert bits of a number Efficiently
- Check whether K-th bit is set or not
- Check whether all the bits are unset in the given range or not
- Check whether a given number is Polydivisible or Not
- Check whether bitwise AND of a number with any subset of an array is zero or not
- Check whether the given number is Wagstaff prime or not
- Check whether the binary equivalent of a number ends with "001" or not
- Check whether a large number is divisible by 53 or not
- Check whether the binary equivalent of a number ends with given string or not
- Find whether a given integer is a power of 3 or not
- Find whether a given number is a power of 4 or not
- Write an Efficient Method to Check if a Number is Multiple of 3
- Check if a number is multiple of 9 using bitwise operators
- Check if binary string multiple of 3 using DFA
- Check whether a given number is even or odd
- Check whether all the bits are set in the given range