“This article has participated in the call for good writing activities, click to view: the back end, the big front end double track submission, 20,000 yuan prize pool waiting for you to challenge!”

Here’s the thing.

One day a code came from the group:Why does the POS need to judge?

I’m familiar with the source code for RocketMQ when I look at it, and if you look at the source code you’ll see it all over the place.

I’m taking the absolute value, and then I’m taking the mod, and I’m going to be positive, so if pos is less than 0, that’s redundant.

So let’s take a lookMath.absSource:As you can see, it’s very simple and crude, and then int, I don’t know if you see the problem?

No more suspense.

Int a maximum of (2 ^ 31) – 1, and the minimum value is 2 ^ 31, so according to the logic of abs, if a is the minimum, the minimum value adding a negative number becomes 2 ^ 31, and a maximum of int can say is (2 ^ 31) – 1, it is greater than the maximum 1, caused upward overflow, So you’re still going to get the minimum.You can try running the above results, so math. abs doesn’t have to be positive! So RocketMQ’s judgment is necessary.

Let me explain why the absolute value of the minimum int is equal to itself.

Int is 32 bits, so for the sake of illustration, I’m going to use 8 bits, but it’s the same thing.

Implementations of numbers in Java are all signed bits. Unlike C, which has an unsigned bit for the unsigned bit, the signed digit implementation uses the highest bit for the signed bit, with 1 representing a negative number and 0 representing a positive number.

Going back to the example above, the positive number is 1 more than the maximum number, so let’s add 1 to the maximum number.

In the figure, the minimum value is obtained by upward overflow, so the absolute value of the minimum value is larger than the maximum value, resulting in upward overflow and the minimum value.

So math.abs (integer.min_value) = integer.min_value

So why would 8 bits represent -128 to 127 and not -127 to 128?

2^8=256 pits, so it can represent 256 numbers. In theory, it can be expressed in any way. You can say -254~1.

But we specify 0 to 255 for unsigned numbers and -128 to 127 for signed numbers.

It’s not impossible to specify minus 127 to 128 if you have signs, but it’s a little bit more cumbersome.

According to the implementation of -128 ~ 127, we can judge the positive and negative of a number only by the highest bit, while -127~128 needs to exclude the special case of 128, that is, before we only need to judge the highest bit, but now we need to judge that the highest bit is 1 and other bits are not all 0.

And that’s going to complicate the circuit design, so we’re going to say -128 to 127 and the same thing for 32 bits.

Math.absExact

This kind of flawed but unprompted math. abs is definitely bad, so there was a math. absExact in JDK 15

So instead of returning a wrong result, you throw something wrong.

The last

A group of friends also said that baidu was asked when the face.

Interview questions are really too many to guard against.

Well, that’s the end of today’s sharing

More interview questions can be seen in my collection of warehouse, each interview question is with the answer, Java foundation has been updated ~

Interview warehouse (with answers) ​

This is the end of today’s sharing, wait for my next article, if you think the article is good.

Three in a row!

Also welcome to pay attention to my personal public account “Yes Level Training Strategy”