当前位置:网站首页>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
边栏推荐
- 【技术随记】
- 生信可视化(part4)--相关性图
- On the matrix order of MNIST linear model
- 文献记录(part106)--GRAPH AUTO-ENCODER VIA NEIGHBORHOOD WASSERSTEIN RECONSTRUCTION
- MFC tabctrl control to modify label size
- pip升级难题(已解决)You are using pip version 19.0.3, however version 22.1.2 is available.
- 关于jinja2 宏定义的小问题
- Unity encrypts ASE game data
- 生信可视化(part1)--柱状图
- Empirical mode decomposition (EMD) and Hilbert Huang transform (HHT)
猜你喜欢
随机推荐
Single precision, double precision and precision (Reprint)
Serial port (RS - 232)
Single cell thesis record (Part12) -- unsupervised spatial embedded deep representation of spatial transcriptomics
单细胞文献学习(part3)--DSTG: deconvoluting spatial transcriptomics data through graph-based AI
生信可视化(part4)--相关性图
汇顶科技GR551x系列开发板已支持OpenHarmony
Machine learning concept sorting (no formula)
DataBricks从开源到商业化踩过的坑
Shengxin visualization (Part1) -- histogram
Single cell paper record (Part11) -- clustermap for multi-scale clustering analysis of spatial gene expression
单细胞论文记录(part6)--SpaGE: Spatial Gene Enhancement using scRNA-seq
五大常考SQL面试题
idea本地运行scope
Array and foreach traversal in C #
Surfer格网文件裁剪
Flink核心功能和原理
单细胞论文记录(part9)--Spatial charting of single-cell transcriptomes in tissues
C#中的数组及Foreach遍历
Markdown中插入类图(classDiagram)
关于MNIST线性模型矩阵顺序问题







