public static int multiply ( final int pa , final int pb , final int m ) { int a = pa ; int b = pb ; int product = 0 ; // Loop to compute product = (a*b)%m. while ( a > 0 ) { // Does the rightmost bit of a == 1? if ( ( a & 1 ) == 1 ) { product += b ; product %= m ; } // Double b modulo m, and // shift a 1 bit to the right. b <<= 1 ; b %= m ; a >>= 1 ; } return product ; }
Multiply two integer values a and b modulo m .