当前位置:网站首页>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
边栏推荐
- NativeForMySQL 连接MySQL8 提示:1251- Client does not support authentication protocol
- 论文解读(CGC)《CGC: Contrastive Graph Clustering for Community Detection and Tracking》
- Worder font page font comparison table
- kettle复制记录到结果和从结果获取记录使用
- 画结果图推荐网址
- 科创人·派拉软件CEO谭翔:零信任本质是数字安全,To B也要深研用户心智
- 运行报错:找不到或无法加载主类 com.xxx.Application
- Practical data Lake iceberg lesson 30 MySQL - > iceberg, time zone problems of different clients
- 智能多线弹性云增加独立的IP地址,如何实现多线功能?
- VMware virtual machines export hard disk vmdk files using esxi
猜你喜欢

力扣-1137.第N个泰波那契数

Worder font page font comparison table

Sigmoid function of activation function

Force buckle - 1137 Nth teponacci number

Nacos Foundation (8): login management

Windows11 安装MySQL服务 提示:Install/Remove of the Service Denied

kettle复制记录到结果和从结果获取记录使用

The maximum number of remote desktop servers has been exceeded

九十八、freemarker框架报错 s.e.ErrorMvcAutoConfiguration$StaticView : Cannot render error page for request

PSCP basic usage
随机推荐
MySQL 的主从复制配置
A detailed explanation of head pose estimation [collection of good articles]
Idea code quality specification plug-in sonarlint
IMEU如何与IMCU相关联(IM 5.5)
Database design of simple voting system
After a circle, I sorted out this set of interview questions..
Practical data Lake iceberg lesson 30 MySQL - > iceberg, time zone problems of different clients
Purpose of IM expression (IM 5.2)
Idea code formatting plug-in save actions
PSCP basic usage
How imeu is associated with imcu (IM 5.5)
Running error: unable to find or load the main class com xxx. Application
How to count fixed assets and how to generate an asset count report with one click
智能多线弹性云增加独立的IP地址,如何实现多线功能?
Fabric 1.0源代码分析(33) Peer #peer channel命令及子命令实现
第五章 使用In-Memory表达式优化查询(IM 5.1)
What is a gateway
Force buckle - 1137 Nth teponacci number
获取钉钉考勤机打卡记录
Interpretation 3 of gdpr series: how do European subsidiaries return data to domestic parent companies?