首页 > 其他 > 详细

ConcurrentHashMap中的2的n次方幂上舍入方法(转)

时间:2015-03-30 01:08:30      阅读:262      评论:0      收藏:0      [点我收藏+]

 

最近看JDK中的concurrentHashMap类的源码,其中有那么一个函数:

技术分享
/**
     * Returns a power of two table size for the given desired capacity.
     * See Hackers Delight, sec 3.2
     */
    private static final int tableSizeFor(int c) {
        int n = c - 1;
        n |= n >>> 1;
        n |= n >>> 2;
        n |= n >>> 4;
        n |= n >>> 8;
        n |= n >>> 16;
        return (n < 0) ? 1 : (n >= MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : n + 1;
    }
技术分享

看不明白这是做什么的,但是后来我查看了《hackers delight》(中译:高效程序的奥秘)这本书,才知道这个函数是用来计算c的上舍入到2的n次幂。书上的函数如下图:

技术分享

已经把《hackers delight》加入书单。

http://www.cnblogs.com/katsura/p/4134737.html

 

ConcurrentHashMap中的2的n次方幂上舍入方法(转)

原文:http://www.cnblogs.com/softidea/p/4376964.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!