有效 Java hashCode() 实现中的位移位

2023-12-25

我想知道是否有人可以详细解释一下

(int)(l ^ (l >>> 32));

在以下 hashcode 实现中执行操作(由 eclipse 生成,但与有效 Java 相同):

private int i;
private char c; 
private boolean b;
private short s;
private long l;
private double d;
private float f;

@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + i;
    result = prime * result + s;
    result = prime * result + (b ? 1231 : 1237);
    result = prime * result + c;
    long t = Double.doubleToLongBits(d);
    result = prime * result + (int) (t ^ (t >>> 32));
    result = prime * result + Float.floatToIntBits(f);
    result = prime * result + (int) (l ^ (l >>> 32));
    return result;
}

Thanks!


基本上,它将 long 的高 32 位与低 32 位进行异或。这是一个分解版本:

// Unsigned shift by 32 bits, so top 32 bits of topBits will be 0,
// bottom 32 bits of topBits will be the top 32 bits of l
long topBits = l >>> 32;

// XOR topBits with l; the top 32 bits will effectively be left
// alone, but that doesn't matter because of the next step. The
// bottom 32 bits will be the XOR of the top and bottom 32 bits of l
long xor = l ^ topBits;

// Convert the long to an int - this basically ditches the top 32 bits
int hash = (int) xor;

To answer your comment: you have a long value which has to be converted into an int to be part of the hash (the result has to only be 32 bits). How are you going to do that? You could just take the bottom 32 bits - but then that means changes in only the top 32 bits would be ignored, which wouldn't make it a very good hash. This way, a change in a single bit of input always results in a change of a single bit of the hash. Admittedly you can still get collisions easily - change both bits 7 and 39, for example, or any other pair of bits 32 positions apart - but that's bound to be the case, given that you're going from 264 possible values to 232.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

有效 Java hashCode() 实现中的位移位 的相关文章

随机推荐

  • 信息检索中的语言模型

    我在 IR 工作 任何人都可以指导我 我该如何实现语言模型Whoosh 我已经应用了TD IDF和BM25 我是红外新手 例如 最简单形式的语言模型只是丢弃所有条件上下文 并独立估计每个术语 这样的模型称为一元语言模型 P uni t 1t
  • Ubuntu 20.04升级,Python缺少libffi.so.6

    我最近将操作系统升级到 Ubuntu 20 04 LTS 现在 当我尝试在 Python 中导入 Numpy 等库时 出现以下错误 ImportError libffi so 6 cannot open shared object file
  • 在 ASP.NET 中模拟会员用户

    在一个通用的asp net网站与Membership Roles并启用哈希密码 我想向管理员提供impersonation以便他们可以浏览该网站that用户会的 该网站应该像该用户登录一样运行 然后能够恢复到他们自己的登录状态 实现这一目标
  • 具有环境变量和可重写 CMD 的 ENTRYPOINT

    这允许 ENV VAR传递给foo ENTRYPOINT usr bin foo ENV VAR 这不会 因为传递的是文字字符串 ENV VAR 而不是环境变量的内容 ENTRYPOINT usr bin foo ENV VAR 这允许覆盖
  • Android 获取 Wifi 连接状态

    我目前正在使用NetworkRequest and NetworkCallback方法 谷歌官方推荐 获取Wifi连接状态 并且部分有效 我期待着onUnavailable 将在以下情况下被调用 close app gt turn off
  • Android - 创建可调整大小的视图

    我目前正在开发一个具有可移动和可调整大小视图的仪表板 我现在遇到的问题是我想通过触摸手势调整视图大小 因此 我想到了一个点 将其添加到选择视图中 可以拖动该点来调整所选视图的大小 这与 Android 主屏幕上的调整大小过程类似 即使经过长
  • 如何更改复选框、滑块、单选按钮和选择组件的强调色

    我想更改复选框 滑块 单选按钮和选择组件的强调色 有什么简单的方法可以做到这一点吗 您可以使用新的accent color财产 如果您正在使用Chrome 版本 93 https www chromestatus com feature 4
  • Symfony2 验证器约束 GreaterThan 其他属性

    我的验证是在 yaml 文件中定义的 如下所示 src My Bundle Resources config validation yml My Bundle Model Foo properties id NotBlank groups
  • Codeigniter 会话数据,它们只是 cookie 吗?

    我将在我的登录系统中使用 Codeigniter 的会话数据 但首先我想了解它们 所以我阅读了用户指南 据我了解 Codeigniter 的会话数据是just饼干 这是真的 这意味着如果用户禁用 cookie 他将无法使用 Codeigni
  • 从整数列表中,获取最接近给定值的数字

    给定一个整数列表 我想找到哪个数字最接近我在输入中给出的数字 gt gt gt myList 4 1 88 44 3 gt gt gt myNumber 5 gt gt gt takeClosest myList myNumber 4 有什
  • pytorch.empty 函数中未初始化的数据是什么

    我正在经历pytorch教程并遇到了pytorch empty功能 有人提到空可以用于未初始化的数据 但是 当我打印它时 我得到了一个值 这和有什么区别pytorch rand它还生成数据 我知道 rand 生成 0 到 1 之间的值 下面
  • “System.MissingMemberException:无法找到服务器工厂”启动 Microsoft.Owin 在 TeamCity 中自托管

    当 Teamcity 运行启动自托管 Web 应用程序的集成测试时 测试失败并显示错误 System MissingMemberException The server factory could not be located for th
  • 在 Jenkins 声明性管道中使用 waitForQualityGate

    Jenkins 2 50 中声明性管道中的以下 SonarQube 6 3 分析阶段失败 并在控制台日志中显示此错误 http pastebin com t2ja23vC http pastebin com t2ja23vC 进一步来说 此
  • Asp.Net Core 2.0+ 中模型验证之前的模型标准化

    我在用着自动模型验证 https blogs msdn microsoft com webdev 2018 02 02 asp net core 2 1 roadmap 参见 更好的输入处理 保持我的控制器干净 所以 HttpPost Pr
  • C# 到 C++ 数组?

    我提供了一个库 我知道它使用 C 我这样导入DLL DllImport pst private static extern int pst get sensor ref PSTSensor sensor PSTSensor 是一个结构体 在
  • Android如何对JSONObjects的JSONArray进行排序

    我制作了一个 jsonobjects 的 jsonarray 现在我需要根据 jsonobjects 中的值对 JSONArray 进行排序 以前我对自定义对象的 ArrayList 进行排序 如下所示 比较器 public class K
  • 函数的推导

    假设我们有一个这样的类模板 template
  • D3D11VA/CUDA 与 NV12 表面的互操作性问题

    我正在尝试构建一个转码管道 其中使用 D3D11VA 对视频进行解码 然后将其引入 CUDA 可以选择使用 CUDA 内核进行修改和 或分析 最后使用 NVENC 进行编码 使用 CUDA NVENC 互操作 想法是在 GPU 上完成所有操
  • 如何防止 Razor 在使用嵌套显示模板时向输入添加前缀?

    当我使用嵌套显示模板并通过 HTML 帮助程序添加输入元素时 Razor 引擎会向字段名称添加前缀 我确实理解这样做是为了保证页面级别输入名称的唯一性 并在回发时重建整个模型 然而 我有许多执行临时操作的小表单 并且我既不需要名称唯一性 也
  • 有效 Java hashCode() 实现中的位移位

    我想知道是否有人可以详细解释一下 int l l gt gt gt 32 在以下 hashcode 实现中执行操作 由 eclipse 生成 但与有效 Java 相同 private int i private char c private