@Rahul: If d is a power of 2, say 2^k, where ^ represents exponentiation,
then the binary representation of d is a 1-bit followed by k 0-bits. Then
d-1 is the binary number comprising k 1-bits. Anding this with n keeps only
the low-order k bits of n, which you can see to be n%d.
Dave
On Wednesday, October 24, 2012 10:36:26 AM UTC-5, rahul sharma wrote:
> unsigned int getModulo(unsigned int n, unsigned int d)
> {
> return ( n & (d-1) );
> }
>
> n:6
> d:4
> ans is 2
>
> d must be power of 2
>
> anyone plz expalin logic behind this..i am not able to get this
>
>
--
You received this message because you are subscribed to the Google Groups
"Algorithm Geeks" group.
To view this discussion on the web visit
https://groups.google.com/d/msg/algogeeks/-/KjTiT0WGFqMJ.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/algogeeks?hl=en.