Optimization Techniques | Set 1 (Modulus) Last Updated : 11 Nov, 2010 Comments Improve Suggest changes Like Article Like Report Modulus operator is costly. The modulus operator (%) in various languages is costly operation. Ultimately every operator/operation must result in processor instructions. Some processors won't have modulus instruction at hardware level, in such case the compilers will insert stubs (predefined functions) to perform modulus. It impacts performance. There is simple technique to extract remainder when a number is divided by another number (divisor) that is power of 2? A number that is an exact power of 2 will have only one bit set in it's binary representation. Consider the following powers of 2 and their binary representations 2 - 10 4 - 100 8 - 1000 16 - 10000 Note those zeros in red color, they contribute to remainder in division operation. We can get mask for those zeros by decrementing the divisor by 1. Generalizing the above pattern, a number that can be written in 2n form will have only one bit set followed by n zeros on the right side of 1. When a number (N) divided by (2n), the bit positions corresponding to the above mentioned zeros will contribute to the remainder of division operation. An example can make it clear, N = 87 (1010111 - binary form) N%2 = N & (2-1) = 1010111 & 1 = 1 = 1 N%4 = N & (4-1) = 1010111 & 11 = 11 = 3 N%8 = N & (8-1) = 1010111 & 111 = 111 = 7 N%16 = N & (16-1) = 1010111 & 1111 = 111 = 7 N%32 = N & (32-1) = 1010111 & 11111 = 10111 = 23 Modulus operation over exact powers of 2 is simple and faster bitwise ANDing. This is the reason, programmers usually make buffer length as powers of 2. Note that the technique will work only for divisors that are powers of 2. An Example: Implementation of circular queue (ring buffer) using an array. Omitting one position in the circular buffer implementation can make it easy to distinguish between full and empty conditions. When the buffer reaches SIZE-1, it needs to wrap back to initial position. The wrap back operation can be simple AND operation if the buffer size is power of 2. If we use any other size, we would need to use modulus operation. Note: Per experts comments, premature optimization is an evil. The optimization techniques provided are to fine tune your code after finalizing design strategy, algorithm, data structures and implementation. We recommend to avoid them at the start of code development. Code readability is key for maintenance. Thanks to Venki for writing the above article. Comment More infoAdvertise with us Next Article Optimization Techniques | Set 1 (Modulus) kartik Follow Improve Article Tags : Bit Magic DSA Practice Tags : Bit Magic Similar Reads Find position of the only set bit Given a number n containing only 1 set bit in its binary representation, the task is to find the position of the only set bit. If there are 0 or more than 1 set bits, then return -1. Note: Position of set bit '1' should be counted starting with 1 from the LSB side in the binary representation of the 8 min read CSES Solutions - Exponentiation Your task is to efficiently calculate values a^b modulo 109+7. Note that in this task we assume that 00=1. Examples: Input: N = 3, queries[][] = {{3, 4}, {2, 8}, {123, 123}}Output: 81256921450052Explanation: The first query is (3 ^ 4) % (109 + 7) = (3 * 3 * 3 * 3) % (109 + 7) = 81The second query is 7 min read Sudo Placement[1.3] | Final Destination Given an array of integers and a number K with initial and final values. Your task is to find the minimum number of steps required to get final value starting from the initial value using the array elements. You can only do add (add operation % 1000) on values to get the final value. At every step, 8 min read Modulo Operations in Programming With Negative Results In programming, the modulo operation gives the remainder or signed remainder of a division, after one integer is divided by another integer. It is one of the most used operators in programming. This article discusses when and why the modulo operation yields a negative result. Examples: In C, 3 % 2 r 15+ min read Set the Left most unset bit Given an integer, set the leftmost unset bit. Leftmost unset bit is the first unset bit after most significant set bit. If all bits (after most significant set bit) are set, then return the number. Examples: Input : 10 Output : 14 10 = 1 0 1 0 // 10 binary 14 = 1 1 1 0 // after set left most unset b 5 min read Minimum number of operations required to reduce N to 1 Given an integer element 'N', the task is to find the minimum number of operations that need to be performed to make 'N' equal to 1. The allowed operations to be performed are: Decrement N by 1.Increment N by 1.If N is a multiple of 3, you can divide N by 3. Examples: Input: N = 4 Output: 2 4 - 1 = 13 min read Set the rightmost unset bit Given a non-negative number n. The problem is to set the rightmost unset bit in the binary representation of n. If there are no unset bits, then just leave the number as it is. Examples: Input : 21 Output : 23 (21)10 = (10101)2 Rightmost unset bit is at position 2(from right) as highlighted in the b 10 min read Increase the count of given subsequences by optimizing the Array Given an array X[] of length N. Which contains the alternate frequency of 0s and 1s in Binary String starting from 0. Then the task is to maximize the count of the "01" subsequence where you can choose two different elements of X[], let's say Xi and Xj such that abs (i - j) = 2, and then swap Xi and 11 min read Closest sum partition (into two subsets) of numbers from 1 to n Given an integer sequence 1, 2, 3, 4, ..., n. The task is to divide it into two sets A and B in such a way that each element belongs to exactly one set and |sum(A) - sum(B)| is the minimum possible. Print the value of |sum(A) - sum(B)|. Examples: Input: 3 Output: 0 A = {1, 2} and B = {3} ans |sum(A) 4 min read Minimum Operations to Reduce X to Zero Given an integer array of nums[] and an integer x. In one operation, you can either remove the leftmost or the rightmost element from the array nums and subtract its value from x. Note that this modifies the array for future operations, the task is to return the minimum number of operations to reduc 9 min read Like