当前位置:网站首页>Detailed interpretation of tab[i = (n - 1) & hash]
Detailed interpretation of tab[i = (n - 1) & hash]
2022-06-22 06:09:00 【Xiaoshuang is handsome enough to drag the Internet speed】
tab[i = (n - 1) & hash] A detailed interpretation of
n representative tab The capacity of the array 16 -1( Array subscript index from 0 Start )
there hash Is a hash(key) Calculated
First, click and operate (&), Is to convert two operands into 2 Base number , according to 1 1 = 0 ,1 0 = 0, 0 0 = 0 Calculate according to the rules of
According to the data of the above procedure , To do the analysis

Current hash:2080,n = 16
(n - 1) & hash = 15 & 2080

Imagine biting and manipulation 1 1 Only then 1, and 15 The binary number of is 0000 0000 0000 1111 front 12 Position as 0 I don't need to see it anymore , No matter 2080 Binary number of 0000 1000 0010 0000 front 12 What is the number of bits , Before the final result 12 Bits are for 0
tab[i = (n - 1) & hash] The design of the , To ensure the i The value is [0,15] In this closed interval
There is another important reason , Namely (n-1)& hash = hash % n , The premise of satisfying this equation is ,n Must be 2 Of a Power
Why? ?
because 2 Of n The power of 10 The base number is converted to 2 Hexadecimal number , There is only one 1, The rest are 0, for instance 16( Decimal system )= 10000( Binary system )
16 = 2^4 Convert to 2 After base n=4 The first one is 1, That is to say 10000, And the front 15 It's through 16-1 Yes, I did , That is to say 10000-1 obtain 01111, At this time will be 2080 Convert to 2 Into the system for 0000 1000 0010 0000, And 01111 Do bit and operation , The final result is 2080%16 = 0
summary When a number satisfies m = 2^n be a % m = a & (m-1)
Many friends here will ask , Why do that ?
First 2 The hexadecimal operation must be faster than the remainder operation , Second, whether it's HashSet,LinkHashSet Maintenance of table The capacity of is 2^n Fang , It also guarantees (n-1)& hash The rationality of design

The above content is only for personal interpretation , If there is any misunderstanding , Please comment on , thank you
边栏推荐
- vcpkg:If you are sure you want to rebuild the above packages, run the command with the --recurse opt
- 【NAND文件系统】UBIFS介绍
- TCP连接细节问题
- idea本地运行scope
- simulink中搭建专家pid控制
- 关于MNIST线性模型矩阵顺序问题
- 单细胞论文记录(part9)--Spatial charting of single-cell transcriptomes in tissues
- 单精度,双精度和精度(转载)
- Single cell paper records (Part8) -- cell2location maps fine grained cell types in spatial transcriptomics
- Markdown中插入类图(classDiagram)
猜你喜欢

pip升级难题(已解决)You are using pip version 19.0.3, however version 22.1.2 is available.

小熊派BearPi-HM Micro正式合入OpenHarmony主干

Le contrôle MFC tabctrl modifie la taille de l'étiquette

Single cell paper record (Part11) -- clustermap for multi-scale clustering analysis of spatial gene expression

生信可视化(part2)--箱线图

经验模式分解(EMD)和希尔伯特-黄变换(HHT)

Empirical mode decomposition (EMD) and Hilbert Huang transform (HHT)

D3D10 截图功能 保存Texture到本地

【自己动手写CPU】异常相关指令的实现

Creating GLSL Shaders at Runtime in Unity3D
随机推荐
不务正业系列7:老照片去除斑点手法
Single cell paper records (part10) -- computational challenges and opportunities in SRT data
Keil调试时设置断点的高级用法
单细胞论文记录(part6)--SpaGE: Spatial Gene Enhancement using scRNA-seq
【雲計算重點複習】
电热水壶坏了别扔,它很容易修好的!
Improve your game‘s performance
BinaryFormatter saving and loading game data for unity
【CPU设计实战】数字逻辑电路设计基础(一)
Subqueries in sqlserver
R language observation log (part24) -- writexl package
EMC的解决
Modeling and Simulation of Radar Seeker Servo System
Single cell paper records (Part8) -- cell2location maps fine grained cell types in spatial transcriptomics
单细胞论文记录(part12)--Unsupervised Spatial Embedded Deep Representation of Spatial Transcriptomics
reduce_sum()中的reduction_indices
Matlab system identification
vcpkg:If you are sure you want to rebuild the above packages, run the command with the --recurse opt
MFC tab control add Icon
单细胞论文记录(part8)--Cell2location maps fine-grained cell types in spatial transcriptomics