You are given an positive integer n as dividend and another number m (form of 2^k), you have to find quotient and remainder without performing actual division.
Input : n = 43, m = 8 Output : Quotient = 5, Remainder = 3 Input : n = 58, m = 16 Output : Quotient = 3, Remainder = 10
In this we are using bitwise representation of a number for understanding the role of division of any number by divisor of form 2^k. All numbers which are power of two includes only 1 set bits in their representation and we will use this property.
For finding remainder we will take logical AND of the dividend (n) and divisor minus 1 (m-1), this will give only the set bits of dividend right to the set bit of divisor which is our actual remainder in that case.
Further, the left part of the dividend (from the position of set bit in divisor) would be considered for quotient. So, from dividend (n) removing all bits right from the position of set bit of divisor will result into quotient, and right shifting the dividend log2(m) times will do this job for finding the quotient.
- Remainder = n & (m-1)
- Quotient = (n >> log2(m) )
Note : Log2(m) will give the number of bits present in the binary representation of m.
Remainder = 3 Quotient = 5
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.
- Count of divisors having more set bits than quotient on dividing N
- Check if given number is a power of d where d is a power of 2
- Find the sum of power of bit count raised to the power B
- Length of longest increasing index dividing subsequence
- Find the remainder when N is divided by 4 using Bitwise AND operator
- Program to find remainder when large number is divided by 11
- Find remainder of array multiplication divided by n
- Generate Array whose sum of all K-size subarrays divided by N leaves remainder X
- Program to find remainder when large number is divided by r
- Maximum frequency of a remainder modulo 2i
- First number to leave an odd remainder after repetitive division by 2
- Check if bitwise AND of any subset is power of two
- Count unordered pairs (i,j) such that product of a[i] and a[j] is power of two
- Minimum absolute difference between N and a power of 2
- Array containing power of 2 whose XOR and Sum of elements equals X
- Count of pairs whose bitwise AND is a power of 2
- Program to find whether a no is power of two
- Smallest power of 2 greater than or equal to n
- Compute modulus division by a power-of-2-number
- Find whether a given number is a power of 4 or not
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.