了解比特币Merkle树实现中的CalctreeWidth
我正在尝试理解此BITSHIFTIFT功能:
/** helper function to efficiently calculate the number of nodes at given height in the merkle tree */
unsigned int CalcTreeWidth(int height) const {
return (nTransactions+(1 << height)-1) >> height;
}
我尝试手工运行该功能,并且可以看到它产生了正确的结果,但我不明白,我了解该部分:(1&lt;&lt; height; )-1)
基本上将n =高度
位设置为1,我不明白的是下一部分。
为什么添加叶子的数量和正确的高度时间会导致该级别的节点数量?
I'm trying to understand this bitshifting function:
/** helper function to efficiently calculate the number of nodes at given height in the merkle tree */
unsigned int CalcTreeWidth(int height) const {
return (nTransactions+(1 << height)-1) >> height;
}
I tried running the function by hand and I can see that it produces correct result but I don't get it, I understand that the part : (1 << height)-1)
basically sets N=height
bits to 1, what I don't understand is the next part.
Why does adding the number of leafs and shifting right height times result in the number of node on that level?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它正在执行
ntransactions&gt;&gt;高度
,但要四舍五入而不是舍入。ntransactions&gt;&gt;高度
ISntransactions/(1&lt;&lt; height)
。通过添加(1&lt;&lt; height)-1
我们做到这一点,以便如果ntransactions
不是1&lt;&lt;&lt;高度
然后结果更大。It's doing
nTransactions >> height
but rounding up instead of rounding down.nTransactions >> height
isnTransactions / (1 << height)
. By adding(1 << height) - 1
we make it so that ifnTransactions
isn't an exact multiple of1 << height
then the result is 1 greater.