This article mainly introduces the integer product calculation function of Java simulation computer, briefly analyzes the principles of computer numerical system conversion and product calculation through displacement, and provides relevant operations of Java simulation computer score calculation with specific examples. For tips, friends who need them can refer to
. The example in this article describes the integer product calculation function of Java simulation computer. Share it with everyone for your reference, the details are as follows:
The principle of computer calculation of integer products:
Implementation code:
package math; public class two { /** * Fundamental method * f(n) = O(n^2) * @param a * @param b * @return */ public static int naiveMul(int a,int b){ int x = 0; //判断a中出现1的位置,每当出现1就将b的移位运算结果加到最终的结果中。 while(a > 0){//n bits if(a%2==1) x = x + b; //n bits a = a>>1; b = b<<1; } return x; } public static void main(String [] args){ System.out.println("脚本之家测试结果:"); System.out.println(naiveMul(20,60)); } }
Running results:
The above is the detailed content of Java imitation computer integer multiplication function. For more information, please follow other related articles on the PHP Chinese website!