Java 我可以使用十进制中负值为正值的字节吗

Java 我可以使用十进制中负值为正值的字节吗,java,byte,signed,Java,Byte,Signed,如果我有以下方法: 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;

如果我有以下方法:

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 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 

}
现在我想用这些方法来计算多项式f(x)=a0+a1x+a2x(pow2)+。。。a2 x(功率k-1),其中这些系数a0、a1、a2 i生成如下:

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 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(a)方法了

如果
add()
FFMulFast()
方法预期为正值,则必须使用:

for (byte b : a) {
        r = add(r, FFMulFast(b & 0xff, xi));
        xi = FFMulFast(xi, x & 0xff);
}

“你为什么不试试看呢?”奥利弗·卡莱斯沃斯没有工作,这就是我为什么要问的原因。但我不确定问题是在于这些系数,还是在于对文件的每个字节使用这些多项式的另一种方法。我没有在这里出现的那个。因此,我不确定错误在哪里,也不确定是否存在其他错误。我确信乘法和加法都很有效。如果你能看看我上面的代码并给我一个clue@johnsmith你能简要说明一下总体目标吗?您使用bytes和getBytes(..)方法背后的原因是什么?那么您应该进行一些调试。@Andreas-您见过Galois字段算法吗?