当前位置:网站首页>Why is the premise of hash% length = = hash & (length-1) that length is the nth power of 2
Why is the premise of hash% length = = hash & (length-1) that length is the nth power of 2
2022-04-23 12:06:00 【Teng Qingshan yyds】
Hash The range of values is -2147483648 To 2147483647, It's about 40 Billion mapping space , But we can't create one 40 Billion size array ,HashMap The default array size is 16.
So I'm getting Hash After the value, you need to remainder the length of the array , The remainder is at the index of the array .
So why do you say hash%length == hash&(length-1) The premise is length yes 2 Of n Power ?
From a binary point of view , Shift a number right 1 Bits are divided by 2, What its remaining binary numbers represent is to divide by 2 The result , Then the removed number is the remainder .
in other words , Shift a number right n position , Is to divide this number by 2 Of n Power , Removed n A digit is the remainder we want .
When the length of an array is 2 Of n In the second place , We let hash To make a surplus on it is to want hash On the far right n digit .
here , The length of the array is expressed in binary numbers 1 Follow behind n individual 0, If it is -1, That is to say length-1, At this time, the binary digital representation is that the front is 0, Follow behind n individual 1.
Then let's hash Follow length-1 Carry out bit and operation , And you get hash After n Bits are the remainder you want .
The reason why hash&(length-1) To get the rest , Instead of doing it directly hash%length The reason is : Computing speed is faster , More efficient .
Reference resources :
Java Gather common knowledge points & Interview question summary ( Next ) | JavaGuide
Binary bitwise AND & And remainder | Perkins4j2 Technology blog
版权声明
本文为[Teng Qingshan yyds]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231202295171.html
边栏推荐
- IMEU如何与IMCU相关联(IM 5.5)
- Database Navigator 使用默认MySQL连接提示:The server time zone value ‘Öйú±ê׼ʱ¼ä’ is unrecognized or repres
- AcWing 1874. Moo encryption (enumeration, hash)
- Chapter 4 specifies the attribute of the inmemory column on the no inmemory table for im enabled filling objects: examples (Part IV of im-4.4)
- Nacos Foundation (7): Configuration Management
- 欣旺达宣布电池产品涨价 此前获“蔚小理”投资超10亿
- Yunna | fixed assets inventory supports multiple inventory methods (asset inventory)
- Metalama简介4.使用Fabric操作项目或命名空间
- 为什么hash%length==hash&(length-1)的前提是 length 是 2 的 n 次方
- Practical data Lake iceberg lesson 30 MySQL - > iceberg, time zone problems of different clients
猜你喜欢

Nacos Foundation (9): Nacos configuration management from single architecture to microservices

IDEA设置版权信息

在 VSCode 中调试 Jest 的测试用例,VSCode调试Jest测试用例报错basedir=$(dirname “$(echo “$0“ | sed -e ‘s,\\,/,g‘)“)解决

Idea database navigator plug-in

科创人·派拉软件CEO谭翔:零信任本质是数字安全,To B也要深研用户心智

面了一圈,整理了这套面试题。。

CGC: contractual graph clustering for community detection and tracking

Sigmoid function of activation function

Resolution due to AMD not found_ ags_ x64. DLL, unable to continue code execution. Reinstallation of the program may solve this problem, Forza horizon 5

Nacos Foundation (6): Nacos configuration management model
随机推荐
Force buckle - 1137 Nth teponacci number
Siri gave the most embarrassing social death moment of the year
在 VSCode 中调试 Jest 的测试用例,VSCode调试Jest测试用例报错basedir=$(dirname “$(echo “$0“ | sed -e ‘s,\\,/,g‘)“)解决
数据库如何填充IM表达式(IM 5.4)
【Redis 系列】redis 学习十三,Redis 常问简单面试题
Idea database navigator plug-in
Sofa weekly | excellent Committee of the year, contributor of this week, QA of this week
Summary of convolution layer and pooling layer
Nacos Foundation (6): Nacos configuration management model
The fourth chapter is about enabling and disabling the im column storage of table space for im enabled filling objects (IM 4.5)
golang之筆試題&面試題01
C# F23.StringSimilarity库 字符串重复度、文本相似度、防抄袭
第四章 为IM 启用填充对象之启用和禁用列(IM-4.3 第三部分)
Nacos Foundation (7): Configuration Management
Fastjson 2 来了,性能继续提升,还能再战十年
In idea Solution to the problem of garbled code in Chinese display of properties file
第五章 使用In-Memory表达式优化查询(IM 5.1)
一文详解头部位姿估计【收藏好文】
第四章 为IM 启用填充对象之为IM列存储启用ADO(IM 4.8)
PSCP 基本使用