Java 位移操作

在HashMap源码中看到使用了位移运算符,所以在此记录这个公式。

    /**
     * The default initial capacity - MUST be a power of two.
     */
    static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16

    /**
     * The maximum capacity, used if a higher value is implicitly specified
     * by either of the constructors with arguments.
     * MUST be a power of two <= 1<<30.
     */
    static final int MAXIMUM_CAPACITY = 1 << 30;
左移运算符(<<)

记住公式:a << b = a * 2^b

右移运算符(>>)

记住公式:a >> b = a / 2^b

你可能感兴趣的:(Java 位移操作)