Home  >  Article  >  Java  >  An example of how Java simulates the computer's integer product calculation function

An example of how Java simulates the computer's integer product calculation function

黄舟
黄舟Original
2017-09-18 09:57:411978browse

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 An example of how Java simulates the computer's integer product calculation function. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn