Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

byte array multiplication

807598Nov 6 2006 — edited Nov 7 2006
Hi everyone:

I want to calculate the exponent for a base number using byte array. I can't use bigInteger class therfore i have to use byte array to do this.

What I am doing currently is multiplying the base number by itself. For example to calculate 4 to the power of 3
I multiple 4 * 4 * 4. I tried to simulate the hand method for multiplication .

For example :
19 * 9
1 - 9 * 9 we get 81 so we leave the reminder (81 % 10) which is 1 and we carry 8 (81/10). We insert one in the array and we carry 8.
2- Then we multiply 9 * 1 + c = 17 because this the end we insert 17 in the array .
The result would be 171.
Now if I tried to multiply 19 * 19 it will be more complex and involves adding and shifting the result every time.
My code is working but it is not good. The performance is bad. If I try to calculate 123 to the power of 52 it took 23 seconds which is very bad. I am new to this your help is appreciated.


Regards
public class Exponent {


	
	//I found this in java forum http://forum.java.sun.com
	 public byte[] add(byte[] lhs, byte[] rhs) {
	        byte[] result = new byte[Math.max(lhs.length, rhs.length)];
	        int remainder = 0;
	        int resI, lhsI, rhsI;
	        for (resI = result.length - 1, lhsI = lhs.length - 1, rhsI = rhs.length - 1;
	             lhsI >= 0 && rhsI >= 0;
	             resI--, lhsI--, rhsI--) {
	            byte[] added = addBytes(lhs[lhsI], rhs[rhsI], remainder);
	            remainder = added[0];
	            result[resI] = added[1];
	        }
	        byte[] longer = lhs.length > rhs.length ? lhs : lhs.length < rhs.length ? rhs : null;
	        int index = lhs.length > rhs.length ? lhsI : lhs.length < rhs.length ? rhsI : 0;
	        if (longer != null) {
	            for (; index >= 0; index--) {
	                byte[] added = addBytes(longer[index], (byte)0, remainder);
	                remainder = added[0];
	                result[index] = added[1];
	            }
	        }
	        if (remainder > 0) {
	            byte[] temp = result;
	            result = new byte[temp.length + 1];
	            result[0] = 1;
	            System.arraycopy(temp, 0, result, 1, temp.length);
	        }
	        return result;
	    }
	 
	    private byte[] addBytes(byte b1, byte b2, int remainder) {
	        byte[] result = new byte[2];
	        int temp = b1 + b2 + remainder;
	        if (temp > 9) {
	            result[0] = 1;
	            result[1] = (byte)(temp - 10);
	        } else {
	            result[0] = 0;
	            result[1] = (byte)temp;
	        }
	        return result;
	    }
	 
	    
	    

	    
	    //this code is use to calculate X to the power of y
		   
		   public void power(short e){
			   byte [] N2 = new byte [] {1,2,3};
			   byte [] rr = null;
			   rr = startM(N2);
			   for (int i=0;i<e - 2;i++){
				   rr = startM(rr);
				   
			   }
			   
			   System.out.println("The result are ");
			   for (int i=0;i<rr.length;i++){
				   System.out.print(rr);
}



}


public byte[] startM(byte [] N2){
byte [] result = null;
byte [] result2 = null;
byte[] baz = null;
byte[] bb = {0};
byte count = 0;
int le = N2.length;


for (int j=N2.length -1 ;j>=0;j--){

result = multiplyTe(N2[j],count,le);
baz = add(result, bb);
bb = baz;
count++;

}

return bb;

}



public byte [] multiplyTe(byte N2, byte count,int len){

byte [] N1 = new byte [] {1,2,3};
byte [] r = new byte [3];
byte [] rr = new byte [len+ + 2+ count];
byte c = 0;
byte reminder;
int k= len + 1 + count ;
byte result;


if (count > 0){

for (int i =0; i< count;i++){
rr[k] = 0;
k--;
}

}


for(int i =N1.length - 1;i>=0;i--){

result = (byte)((short)N2 * (short)N1[i] + c);
c = 0;
if ((result > 9) && (i >0)){
reminder = (byte)(result % 10);
rr[k] = (byte)(result % 10);
c = (byte)(result / 10);
k -- ;

}else{

if (i == 0 & result>9 ){

rr[k] = (byte)(result % 10);
k --;
c = (byte)(result / 10);;
rr[k] = c;

}else{
rr[k] = result ;

k -- ;
}
}

}

return rr;
}




public static void main(String [] args){
Exponent ex = new Exponent();

System.out.println("start ");
long start,stop,elapsed;

start = System.currentTimeMillis();
ex.power((short)52);
stop = System.currentTimeMillis();
System.out.println( " it took : " + ((stop - start)/1000) + "");


}

}




Message was edited by:
maxjack@mx

Message was edited by:
maxjack@mx
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 5 2006
Added on Nov 6 2006
3 comments
1,226 views