Given a number **N**, the task is to find the remainder when N is divided by 4 using Bitwise AND operator.

**Examples:**

Input:N = 98Output:2Explanation:98 % 4 = 2. Hence the output is 2.Input:200Output:0Explanation:200 % 4 = 0. Hence output is 0.

**Naive approach:**

For solving the above-mentioned problem we can use a naive method by using the **Modulo (%) operator** to find the remainder. But, the Modulo operator is computationally expensive and the method is inefficient.

**Efficient Approach:**

If we carefully observe the binary representation of N and its remainder with 4, we observe that remainder is simply the rightmost two bits in N. To get the rightmost two bits in number N, we perform bitwise AND (&) with 3 because 3 in binary is 0011. To understand the approach better let us have a look at the image below:

**Below is the implementation of the above approach:**

## C

`// C implementation to find N ` `// modulo 4 using Bitwise AND operator ` ` ` `#include <stdio.h> ` ` ` `// Function to find the remainder ` `int` `findRemainder(` `int` `n) ` `{ ` ` ` ` ` `// Bitwise AND with 3 ` ` ` `int` `x = n & 3; ` ` ` ` ` `// return x ` ` ` `return` `x; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` ` ` `int` `N = 43; ` ` ` `int` `ans = findRemainder(N); ` ` ` ` ` `printf` `(` `"%d"` `, ans); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## C++

`// C++ implementation to find N ` `// modulo 4 using Bitwise AND operator ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the remainder ` `int` `findRemainder(` `int` `n) ` `{ ` ` ` `// Bitwise AND with 3 ` ` ` `int` `x = n & 3; ` ` ` ` ` `// Return x ` ` ` `return` `x; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` ` ` `int` `N = 43; ` ` ` ` ` `int` `ans = findRemainder(N); ` ` ` ` ` `cout << ans << endl; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation to find N ` `// modulo 4 using Bitwise AND operator ` ` ` `class` `Main { ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` ` ` `int` `N = ` `43` `; ` ` ` ` ` `int` `ans = findRemainder(N); ` ` ` ` ` `System.out.println(ans); ` ` ` `} ` ` ` ` ` `// Function to find the remainder ` ` ` `public` `static` `int` `findRemainder(` `int` `n) ` ` ` `{ ` ` ` `// Bitwise AND with 3 ` ` ` `int` `x = n & ` `3` `; ` ` ` ` ` `// return x ` ` ` `return` `x; ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python 3

`# Python 3 implementation to find N ` `# modulo 4 using Bitwise AND operator ` ` ` `# Function to find the remainder ` `def` `findRemainder(n): ` ` ` `# Bitwise AND with 3 ` ` ` `x ` `=` `n & ` `3` ` ` ` ` `# Return x ` ` ` `return` `x ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `N ` `=` `43` ` ` ` ` `ans ` `=` `findRemainder(N) ` ` ` ` ` `print` `(ans) ` ` ` `# This code is contributed by Surendra_Gangwar ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation to find N ` `// modulo 4 using Bitwise AND operator ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` ` ` `int` `N = 43; ` ` ` ` ` `int` `ans = findRemainder(N); ` ` ` ` ` `Console.Write(ans); ` ` ` `} ` ` ` ` ` `// Function to find the remainder ` ` ` `public` `static` `int` `findRemainder(` `int` `n) ` ` ` `{ ` ` ` `// Bitwise AND with 3 ` ` ` `int` `x = n & 3; ` ` ` ` ` `// return x ` ` ` `return` `x; ` ` ` `} ` `} ` ` ` `# This code is contributed by chitranayal ` |

*chevron_right*

*filter_none*

**Output:**

3

**Time Complexity:** O(1)

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:

- Program to find remainder when large number is divided by 11
- Program to find remainder when large number is divided by r
- Find the remainder when First digit of a number is divided by its Last digit
- Total pairs in an array such that the bitwise AND, bitwise OR and bitwise XOR of LSB is 1
- Largest number by which given 3 numbers should be divided such that they leaves same remainder
- Minimum number of operations on a binary string such that it gives 10^A as remainder when divided by 10^B
- Count of integers obtained by replacing ? in the given string that give remainder 5 when divided by 13
- Largest number less than or equal to Z that leaves a remainder X when divided by Y
- Check if the remainder of N-1 factorial when divided by N is N-1 or not
- Smallest subarray whose product leaves remainder K when divided by size of the array
- Program to find remainder without using modulo or % operator
- Find subsequences with maximum Bitwise AND and Bitwise OR
- Count ways to generate pairs having Bitwise XOR and Bitwise AND equal to X and Y respectively
- Maximize sum of squares of array elements possible by replacing pairs with their Bitwise AND and Bitwise OR
- Multiply any Number with 4 using Bitwise Operator
- Find Quotient and Remainder of two integer without using division operators
- Leftover element after performing alternate Bitwise OR and Bitwise XOR operations on adjacent pairs
- Minimum possible Bitwise OR of all Bitwise AND of pairs generated from two given arrays
- Count pairs with bitwise XOR exceeding bitwise AND from a given array
- Program to find Quotient And Remainder in Java

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.