2016-03-06 27 views
-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 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(一)方法

+0

爲什麼你不試試看看? –

+0

@OliverCharlesworth不工作,這就是爲什麼我問。但是我不確定問題出在這些係數上還是另一種方法對文件的每個字節使用這些多項式。我沒有在這裏出現的那個。所以我不知道錯誤在哪裏或另一個錯誤。我相信的是,乘法和加法方法運行良好。我會很感激,如果你能看看我的代碼,並給我一個線索 –

+0

@johnsmith你能簡要說明總體目標嗎?你使用字節和getBytes(..)方法的原因是什麼? –

回答

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

如果add()FFMulFast()方法期望正值,您將不得不使用:

​​
相關問題