Here you can find the source of multiply(byte[] a, byte b)
Parameter | Description |
---|---|
a | a parameter |
b | a parameter |
public static byte[] multiply(byte[] a, byte b)
//package com.java2s; /**//from ww w . j a v a2s . co m * ?????????? unsigned ???????????????????????????????????????? * * License : MIT License */ public class Main { /** * a * b * @param a * @param b * @return */ public static byte[] multiply(byte[] a, byte b) { if (a == null) throw new IllegalArgumentException("a should not be null"); int len = a.length; byte[] result = new byte[len]; int carry = 0; int i2 = byte2int(b); for (int i = len - 1; 0 <= i; i--) { int i1 = byte2int(a[i]); // 0 <= i1 <= 255(0xff) int col = i1 * i2 + carry; carry = (col >> 8); result[i] = int2byte(col); } return result; } /** * a * b * @param a * @param b * @return */ public static byte[] multiply(byte[] a, byte[] b) { if (a == null) throw new IllegalArgumentException("a should not be null"); if (b == null) throw new IllegalArgumentException("b should not be null"); if (a.length != b.length) throw new IllegalArgumentException( "byte array length should be same"); int len = a.length; byte[] result = new byte[len]; for (int offset = len - 1; 0 <= offset; offset--) { int i2 = byte2int(b[offset]); // 0 <= i1 <= 255(0xff) int carry = 0; for (int i = len - 1; 0 <= i; i--) { int pos = offset - (len - 1 - i); if (pos < 0) break; int i1 = byte2int(a[i]); // 0 <= i1 <= 255(0xff) int r = byte2int(result[pos]); int col = r + i1 * i2 + carry; carry = (col >> 8); result[pos] = int2byte(col); } } return result; } public static int byte2int(byte b) { return ((int) b & 0x000000ff); } public static byte int2byte(int i) { return (byte) (i & 0x000000ff); } }