![](/img/trans.png)
[英]Given a number n and two integers p1,p2 determine if the bits in position p1 and p2 are the same or not. Positions p1,p2 and 1 based
[英]How to extract the p MSB bits of a given number
好的,所以我正在实现一个使用乘法方法的 hash function。
我得到了一个 K 和一个 S(其中 S = A * 2^word)。 我找到了我的 p,我需要做的就是从 KS 的乘积中提取 p 的最高有效位。
因此,假设 KS = 51 表示为二进制的 110011 和 p = 4; 我需要提取 4 个最高有效位,即 1100 或 12,理论上这将是 h(k) 用于乘法 hash 函数!
我怎么可能会这样做呢?
一个简单的
int tempHold = k * s;
tempHold << p;
不管用。
您可以取所述数字的对数基数 2 的底数,以获得该数字中的位数 N,然后按 (N -p) 执行右移 (>>)。 这将留下 p 个最高有效位。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.