How to fast calculate ( I mod N)?

Given integer I and an integer N which is power of 2, how does it work faster to calculate “I mod N”?
OpenJDK’s java.util.HashMap.indexFor method gives us a best solution for it.
It simply calculates ” I (bitwise AND) (N-1) “.


1. “HashMap.” Available: [Online]

2. “HashMap implementation in Java. How does the bucket index calculation work?” Stackoverflow. Available: [Online]

This entry was posted in Java, Programming, 程式設計. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *