Note that there are some explanatory texts on larger screens.

plurals
  1. POMultiplication of 2 positive numbers giving a negative result
    primarykey
    data
    text
    <blockquote> <p><strong>Possible Duplicate:</strong><br> <a href="https://stackoverflow.com/questions/3001836/how-does-java-handle-integer-underflows-and-overflows-and-how-would-you-check-fo">How does Java handle integer underflows and overflows and how would you check for it?</a><br> <a href="https://stackoverflow.com/questions/6050796/multiplication-of-two-ints-gets-negativ">Multiplication of two int’s gets negativ</a> </p> </blockquote> <p>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 <code>form f(i) = (a*i + b) % m</code> 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.</p> <p>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 <code>ArrayOutOfBoundsException</code>.</p> <p><code>m</code> is the bit array length, <code>z</code> is the number of hash functions being used, <code>S</code> is the set of values which are members of this bloom filter and <code>H</code> stores the values of a and b for the hash functions f1, f2, ..., fz.</p> <pre><code>public static int[] makeBitArray(int m, int z, ArrayList&lt;Integer&gt; S, int[] H) { int[] C = new int[m]; for (int i = 0; i &lt; z; i++) { for (int q = 0; q &lt; 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; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload