How to Compute n % 2^k in Java Using Binary and Bitwise Methods?

Last Updated : 01 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In Java, sometimes we need to divide one number by another number to find out the remainder. But the most interesting case is when we need to divide by power of 2. The most standard approach to do this using the modulus operator (%) and the fastest approach is to do this by bitwise operations.

What is Modulus by Powers of 2?

When dividing a number n by powers of 2, we can use binary numbers. For example:

  • n % 2 gives us the remainder when dividing by 2, which is the last bit of the binary form of n.
  • n % 4 gives us the remainder when dividing by 4, which is the last two bits.
  • n % 8 gives us the remainder when dividing by 8, which is the last three bits, and so on.

Because of this, we don't need to perform complete division, we can simply work with the last few bits of a binary number to get the result.

Java Program of Modulus by Power of 2

Now, we are going to discuss the approaches for modulus by the power of 2.

  • Using Binary String
  • Using Bitwise Operations for Modulus by Powers of 2

Approach 1: Using Binary String

We can use Integer.toBinaryString() method to convert a number to its binary form and then we get the last few bits based on the power of 2 we are interested in. Below is the Java program to demonstrate the same.

Example:

Java
// Java program to compute 
// modulus by a power of 2

class Geeks {
    public static void main(String[] args) {
        int num = 15;

        // Powers of 2 (1, 2, 3)
        int power1 = 1;
        int power2 = 2;
        int power3 = 3;

        // Convert the number to binary
        String b = Integer.toBinaryString(num);
        int l = b.length();

        // Get the last 'k' bits
        String r1 = b.substring(l - power1);
        String r2 = b.substring(l - power2);
        String r3 = b.substring(l - power3);

        // Convert the binary substring back to integer
        int res1 = Integer.parseInt(r1, 2);
        int res2 = Integer.parseInt(r2, 2);
        int res3 = Integer.parseInt(r3, 2);

        // Print the results
        System.out.println(num + "%" + "2^(" + power1 + ") = " + res1);
        System.out.println(num + "%" + "2^(" + power2 + ") = " + res2);
        System.out.println(num + "%" + "2^(" + power3 + ") = " + res3);
    }
}

Output
15%2^(1) = 1
15%2^(2) = 3
15%2^(3) = 7

Explanation: In the above example, we are using binary operations to find the remainder when dividing the number 15 by different powers of 2. First we need to number 15 to its binary form. Then, for each value of k, we take the last k bits of this binary number. These bits are then converted back to an integer. For example 15 % 2^1 extracts the last bit which gives 1 and 15 % 2^2 extracts the last two bits which gives 3,and 15 % 2^3 extracts the last three bits, resulting in 7.


Approach 2: Using Bitwise Operations for Modulus by Power of 2

The fastest way to compute is by using bitwise operations. This approach avoids converting the number to a binary string which makes it more efficient for large numbers.

Example:

Java
// Java Program to Compute Modulus by 
// Powers of 2 Using Bitwise Operations


public class Geeks {
    public static void main(String[] args) {
        int n = 15;

        // Powers of 2 (1, 2, 3)
        int p1 = 1;
        int p2 = 2;
        int p3 = 3;

        // Using bitwise AND to calculate modulus by power of 2
        int r1 = n & ((1 << p1) - 1);
        int r2 = n & ((1 << p2) - 1);
        int r3 = n & ((1 << p3) - 1);

        // Print the results
        System.out.println(n + "%" + "2^(" + p1 + ") = " + r1);
        System.out.println(n + "%" + "2^(" + p2 + ") = " + r2);
        System.out.println(n + "%" + "2^(" + p3 + ") = " + r3);
    }
}

Output
15%2^(1) = 1
15%2^(2) = 3
15%2^(3) = 7

Explanation: In the above example, we are using bitwise operations to find the remainder when dividing the number 15 by different powers of 2. We are working with the binary version of 15. For each power of 2 we are creating a special mask to focus on the last few digits of binary number and then we are using AND operator to get the remainder.


Next Article
Article Tags :
Practice Tags :

Similar Reads