简体   繁体   English

什么(int&Integer.MAX_VALUE)%int在Java中做什么?

[英]What does (int & Integer.MAX_VALUE) % int do in Java?

In Java, I encountered the following line: 在Java中,我遇到了以下行:

e.g.: (1 & Integer.MAX_VALUE) % 4
e.g.: (2 & Integer.MAX_VALUE) % 5

What does it do? 它有什么作用? I have tried the code, but I couldn't comprehend its purpose or functionality. 我已经尝试过代码,但我无法理解它的用途或功能。 What is the code trying to check? 试图检查的代码是什么?

Basically, it's (int & Integer.MAX_VALUE) % int . 基本上,它是(int & Integer.MAX_VALUE) % int

Actual code (from Hadoop training class): 实际代码(来自Hadoop培训课程):

public int getPartition(StringPairWritable key, Text value, int numReduceTasks) {
    return (key.getLeft().hashCode() & Integer.MAX_VALUE) % numReduceTasks;
}

i & Integer.MAX_VALUE does the same thing as this code: i & Integer.MAX_VALUE与此代码的作用相同:

if(i < 0) {
    i = (i + Integer.MAX_VALUE + 1);
}

The % is a regular remainder operation. %是常规余数运算。

It's a quick way of ensuring that an integer is positive if you don't care about it's actual value (eg if you want to turn random numbers that can be both positive and negative into only positive values). 如果您不关心它的实际值(例如,如果您想将可以是正数和负数的随机数转换为正值),这是确保整数为正的快速方法。

The Integer.MAX_VALUE is 0x7FFFFFFF . Integer.MAX_VALUE0x7FFFFFFF Thus num & Integer.MAX_VALUE clears the highest bit in num . 因此num & Integer.MAX_VALUE清除最高位num The % numReduceTasks is normal remainder after division by numReduceTasks . % numReduceTasks是除以numReduceTasks后的正常余数。

This is done to convert the signed number to the non-negative number and then get the evenly distributed value from 0 to numReduceTasks-1 . 这样做是为了将有符号数转换为非负数,然后获得从0numReduceTasks-1的均匀分布值。 Note that if you write Math.abs(key.getLeft().hashCode()) % numReduceTasks you may get negative number if hashCode() happens to be Integer.MIN_VALUE as Math.abs(Integer.MIN_VALUE) is still Integer.MIN_VALUE . 请注意,如果您编写Math.abs(key.getLeft().hashCode()) % numReduceTasks ,如果hashCode()恰好是Integer.MIN_VALUE ,则可能会得到负数,因为Math.abs(Integer.MIN_VALUE)仍然是Integer.MIN_VALUE So & Integer.MAX_VALUE is a safer alternative. 所以& Integer.MAX_VALUE是一个更安全的选择。

There are two parts here: 这里有两个部分:

  • What the function actually does from a Java perspective, and 从Java的角度来看,函数实际上了什么,以及
  • What purpose the function actually serves , from a Hadoop perspective. 什么目的,功能其实供应 ,从Hadoop的角度。

Let's cover the Java side of it first. 让我们首先介绍它的Java方面。 It's fairly straightforward bitwise math, in that it clears the sign bit and turns the value into a positive integer. 它是相当简单的逐位数学运算,因为它清除符号位并将值转换为正整数。

That's easy enough to demonstrate here; 这很容易在这里展示; let's assume that our key is -128876912, which is 0xF8517E90 . 我们假设我们的密钥是-128876912,即0xF8517E90 The max value for an int is 0x7FFFFFFF . int的最大值是0x7FFFFFFF

If we look at the actual math operation, the sign bit is cleared (along with quite a few other bits, in this case), and we get a positive integer value. 如果我们查看实际的数学运算,符号位将被清除(在这种情况下还有很多其他位),我们得到一个正整数值。

1111 1000 0101 0001 0111 1110 1001 0000
0111 1111 1111 1111 1111 1111 1111 1111
---------------------------------------
0111 1000 0101 0001 0111 1110 1001 0000

If the value is positive, then the net result is that we get back the same value. 如果值为正,则最终结果是我们返回相同的值。

This is important, since a hash code can come back negative; 这很重要,因为哈希码可能会返回负数; I don't believe that you want a negative value for a reason that's important a bit later. 我不相信你想要一个负值,因为一个重要的原因稍后。

For the partitioning bit, this is a bit more Hadoop knowledge than I can truly claim, but after reading the docs , it informs you which partition the value falls under. 对于分区位,这比我真正声称的Hadoop知识要多一些,但在阅读完文档后 ,它会通知您该值属于哪个分区。 That is where the modulo comes in; 这就是模数的来源; you're guaranteed to get a value between [0, partition) , thus specifying which reducer the particular bit of data is processed by. 你可以保证在[0, partition)之间得到一个值,从而指定哪个reducer处理特定的数据位。

From my reading of it, this is one of the default supplied partitioners, and may not be entirely suitable for your uses (you may want to group your data in a different way , for instance). 从我的阅读中,这是默认提供的分区程序之一,可能不完全适合您的使用(例如,您可能希望以不同的方式数据进行分组 )。

If the expression in the code is really 如果代码中的表达式确实存在

(i & Integer.MAX_VALUE) % j

it computes the residue class of i mod j . 它计算i mod j的残差类。 Note that i % j is the remainder of a division, a symmetric (wrt i == 0) function, whereas the residue class, which is a periodic function. 注意, i % j是除法的余数,对称(wrt i == 0)函数,而残差类是周期函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM