Home > Java > javaTutorial > Java imitation computer integer multiplication function

Java imitation computer integer multiplication function

巴扎黑
Release: 2017-09-18 11:37:33
Original
2074 people have browsed it

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));
  }
}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template