Question

In the following Java program I cannot understand what this line does:

wert = (wert * mult + inkr) & 0x7FFFFFFFL;

I understand what the bitwise operators do in conditions, but there are principally two numbers (the hexadecimal is the maximum value of Integers in Java). I do not understand, why the & 0x7FFFFFFFL; has even some influence in this line. In my opinion, the variable wert should simply have the value of (wert * mult + inkr) because it's true. Though I figured out that the & 0x7FFFFFFFL; obviously does have some influence only if (wert * mult + inkr) is negative. Why and what exactly happens in this line?

Annotation: This should be a program for simulating a lottery drawing. I am aware of the error in the program, right where the comment is. But this is not relevant for me right now. Still it would be great if someone could me tell following not really Java-specific question: what sense got the variables mult and inkr?

public static void main(String args[]) {
    int kugeln = 49;
    int ziehen = 6;
    int mult = 1103515245;
    int inkr = 12345;
    long wert = System.currentTimeMillis();
    int zahlen[] = new int[kugeln];

    for(int i = 0; i < kugeln; i++) {
        zahlen[i] = i + 1;
        wert = (wert * mult + inkr) & 0x7FFFFFFFL;
    }

    for(int i = 0; i < ziehen; i++) {
        int index = (int)(wert / 10) % (49 - i);
        int temp = zahlen[49 - i]; // Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 49
        zahlen[49 - i] = zahlen[index];
        zahlen[index] = temp;
        wert = (wert * mult + inkr) & 0x7FFFFFFFL;
    }

    int superzahl = (int)(wert / 10) % 10;
    for(int i = 0; i < ziehen; i++) {
        System.out.println(zahlen[49 - i]);
    }

    System.out.println(superzahl);
}
Was it helpful?

Solution

Using bitwise AND with non-booleans will bitwise AND together all of the bits of the two numbers one by one.

In this case, the number 0x7FFFFFFFL is the hexadecimal representation of a number that is a 0 bit followed by 31 1 bits:

01111111111111111111111111111111

By ANDing this with an integer, you preserve the lower 31 bits (since 1 & x = x for any x) and clear the highest bit (since 0 & x = 0 for any x). Since Java uses a 32-bit signed two's-complement representation, this has the effect of clearing the sign bit, forcing the number to be positive.

My guess is that this program is using some sort of rolling hash function where the resulting number has to be positive. To do this, the code constantly updates the integer by combining it with more and more information, and at each step forces the number to be positive by clearing the sign bit.

Hope this helps!

OTHER TIPS

Anding with 0x7FFFFFFFL results in only the 31 lowest bits getting included, the others get set to 0. The result is always positive because the sign bit is masked out.

int temp = zahlen[49 - i]; // Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 49

i is starting from 0 so zahlen[49 ] is called

 int zahlen[] = new int[kugeln];



int kugeln = 49;



int a[] = new int [49];

a[49] will throw an exception always. Fom there is your exception.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top