Multiplication of 2 positive numbers giving a negative result
Posted
by
krandiash
on Stack Overflow
See other posts from Stack Overflow
or by krandiash
Published on 2012-09-30T09:33:18Z
Indexed on
2012/09/30
9:37 UTC
Read the original article
Hit count: 327
My program is an implementation of a bloom filter. However, when I'm storing my hash function results in the bit array, the function (of the form f(i) = (a*i + b) % m where a,b,i,m are all positive integers) is giving me a negative result. The problem seems to be in the calculation of a*i which is coming out to be negative.
Ignore the print statements in the code; those were for debugging. Basically, the value of temp in this block of code is coming out to be negative and so I'm getting an ArrayOutOfBoundsException.
m is the bit array length, z is the number of hash functions being used, S is the set of values which are members of this bloom filter and H stores the values of a and b for the hash functions f1, f2, ..., fz.
public static int[] makeBitArray(int m, int z, ArrayList<Integer> S, int[] H)
{
int[] C = new int[m];
for (int i = 0; i < z; i++)
{
for (int q = 0; q < S.size() ; q++)
{
System.out.println(H[2*i]);
int temp = S.get(q)*(H[2*i]);
System.out.println(temp);
System.out.println(S.get(q));
System.out.println(H[2*i + 1]);
System.out.println(m);
int t = ((H[2*i]*S.get(q)) + H[2*i + 1])%m;
System.out.println(t);
C[t] = 1;
}
}
return C;
}
Any help is appreciated.
© Stack Overflow or respective owner