73 lines
1.5 KiB
Java
73 lines
1.5 KiB
Java
/*
|
|
* To change this template, choose Tools | Templates
|
|
* and open the template in the editor.
|
|
*/
|
|
|
|
package praktikum1;
|
|
|
|
/**
|
|
* Klasse um zwei Zahlen auf agyptische Art zu multiplizieren
|
|
*/
|
|
public class MUL {
|
|
|
|
/**
|
|
* a und b; haben den Standartwert 0
|
|
*/
|
|
private int a = 0;
|
|
private int b = 0;
|
|
|
|
/**
|
|
* Konstruktoren
|
|
*/
|
|
MUL(){};
|
|
MUL(int a, int b){
|
|
this.a = a;
|
|
this.b = b;
|
|
}
|
|
|
|
/**
|
|
* Multipliziert a und b, welche bei Initialisierung des Objekts
|
|
* übergeben wurden auf agyptische Art.
|
|
*
|
|
* @return Ergebniss der Multiplikation
|
|
*/
|
|
public int mul_agy(){
|
|
return mul_agy(a,b);
|
|
}
|
|
|
|
/**
|
|
* Multipliziert zwei Zahlen auf
|
|
* agyptische Art
|
|
*
|
|
* @param a Multiplikant a
|
|
* @param b Multiplikant b
|
|
* @return Ergebniss der Multiplikation
|
|
*/
|
|
public int mul_agy(int a, int b)
|
|
{
|
|
if(b == 0){ //Rekursionsanker
|
|
return 0;
|
|
}
|
|
|
|
if((b & 0x000001) == 0x000001){ //überprüfe ob die Zahl ungerade ist
|
|
return a + mul_agy(a<<1,b>>>1); //in ia32 muss a auf den stack geschoben werden
|
|
}
|
|
|
|
return mul_agy(a<<1,b>>>1); // verwerfe a, da kein divisionsrest
|
|
}
|
|
|
|
/**
|
|
* Programm-Einstiegspunkt
|
|
*
|
|
* @param args the command line arguments
|
|
*/
|
|
public static void main(String[] args) {
|
|
|
|
MUL inst = new MUL(2,5); //neues MUL Objekt
|
|
|
|
System.out.println(inst.mul_agy()); //berechne und gebe Ergebniss aus
|
|
|
|
}
|
|
|
|
}
|