Why does an ordinary loop in Java get 0 when multiplying continuously from 10 to 99?

This is a very simple piece of Java code:

public class HelloWorld{

    public static void main(String []args){

        int product = 1;

        for (int i = 10; i <= 99; i++) {

            product *= i;

        }

        System.out.println(product);

    }

}

Why is the result 0?

Problem phenomenon

Students who are painful may find the law of execution of this program:

1 * 10 = 10

10 * 11 = 110

110 * 12 = 1320

1320 * 13 = 17160

17160 * 14 = 240240

240240 * 15 = 3603600

3603600 * 16 = 57657600

57657600 * 17 = 980179200

……

-1342177280 * 40 = -2147483648

-2147483648 * 41 = -2147483648

-2147483648 * 42 = 0

0 * 43 = 0

0 * 44 = 0

……

0 * 97 = 0

0 * 98 = 0 The program has output 0 since 42, so the result of multiplying numbers after 42 is obvious. From the results, it is found that the sign of the product has changed in an incomprehensible way, indicating that the product has overflowed, and it also shows that Java does not care about the overflow of integers.

To answer the question, please remember that Java's int type is a 32-bit signed two's complement number type (Translator's Note: 64 is the same for jdk). This is what each step of multiplication does inside the computer:

Insert picture description here The label (1) is the actual decimal result.

Mark (2) the internal representation result of hexadecimal and decimal system. The int type will only store the lower 32 bits of data.

The label (3) is the complement of the label (2).

If you are curious about where 0 comes from, please take a closer look at the result of the binary representation above. Attentive students will notice:

Any number multiplied by an even number gives an even number.

Multiplying an even number by an even number will shift the binary digits to the left as a whole, and 0 will fill the space from the right.

Multiplying even and odd numbers does not change the number of zeros on the far right.

When the multiplication is performed enough times, there will be more and more 0 bits on the right. Finally, when the product is continuously multiplied to 42, the low 32 bits of the binary representation of the product are all 0, so the int will be 0.

Problem extension

Now that we know the cause of the problem, we can use another variable to do the same operation, taking byte as an example.

Java's byte variable is an 8-bit signed number, which is also represented by one's complement. It can be seen from the result table above that when continuously multiplying from 10 to 16, the lower 8 bits of the binary result are all 0, so the byte variable at this time is 0. When multiplying to 15, the lower 8 bits are 10010000. Remember how to find the original code from the complement? Very simple, the sign bit remains unchanged, the other bits are inverted and added 1 to get 11110000, which is -112. If you are interested, please verify the result on your machine. Interested students can join the technical discussion group: 626267345file

Guess you like

Origin blog.csdn.net/weixin_46577306/article/details/108285637