我可以使用它在小数有负值为正值
问题描述:
字节
,如果我有如下这些方法:我可以使用它在小数有负值为正值
public int add (int a , int b) {
assert(a>= 0 && a < 256 && b >= 0 && b < 256);
// the addition is simply an xor operation in GF (256) since we are working on modulo(2) then 1+1=0 ; 0+0=0; 1+0=0+1=1;
return a^b;
}
第二种方法是:
public int FFMulFast(int a, int b){
int t = 0;;
if (a == 0 || b == 0)
return 0;
// The multiplication is done by using lookup tables. We have used both logarithmic and exponential table for mul
// the idea is firstly look to Logarithmic table then add their powers and find the corresponding of this to exponential table
t = (Log[(a & 0xff)] & 0xff) + (Log[(b & 0xff)] & 0xff);
if (t > 255) t = t - 255;
return Exp[(t & 0xff)];
}
现在我想用这些方法用于计算多项式f(x)= A0 + A1X + A2-X(POW 2)+ ... A2-X(POW K-1)其中,这些系数a0,A1,A2我已经产生如下图所示:
public void generate (int k) {
byte a [] = new byte [k];
Random rnd = new SecureRandom() ;
a.nextBytes (a); // the element of byte array are also negative as for example -122; -14; etc
}
现在我想计算我的多项式,但我不确定它是否因为这个负系数而起作用。我知道JAVA只支持有符号字节,但我不确定下面的方法是否能正常工作:
private int evaluate(byte x, byte[] a) {
assert x != 0; // i have this x as argument to another method but x has //only positive value so is not my concern , my concern is second parameter of //method which will have also negative values
assert a.length > 0;
int r = 0;
int xi = 1;
for (byte b : a) {
r = add(r, FFMulFast(b, xi));
xi = FFMulFast(xi, x);
}
return r;
}
有没有什么建议?此外,如果这个人是不工作任何人都可以建议我如何把以积极的负值,而无需使用屏蔽,因为它将被更改的数据类型为int,然后我不能使用的getBytes(一)方法
答
for (byte b : a) {
r = add(r, FFMulFast(b, xi));
xi = FFMulFast(xi, x);
}
如果add()
和FFMulFast()
方法期望正值,您将不得不使用:
为什么你不试试看看? –
@OliverCharlesworth不工作,这就是为什么我问。但是我不确定问题出在这些系数上还是另一种方法对文件的每个字节使用这些多项式。我没有在这里出现的那个。所以我不知道错误在哪里或另一个错误。我相信的是,乘法和加法方法运行良好。我会很感激,如果你能看看我的代码,并给我一个线索 –
@johnsmith你能简要说明总体目标吗?你使用字节和getBytes(..)方法的原因是什么? –