Java位运算符

200 阅读3分钟

常见的运算符:

& 与

两个数都转为二进制,然后从高位开始比较,如果两个数都为1则为1,否则为0。

或(|)

两个数只要有一个为1则为1,否则就为0。

非(~),反义词

如果位为0,结果是1,如果位为1,结果是0

异或 (^)

相同为0,不相同则为1

说明:以上运算符均为位运算符,计算机中存储的时数字的补码,正数的补码为本身,负数的补码是:符号位不变,其他取反加一。

左移运算符<<

  • 规则:

    按二进制形式把所有的数字向左移动对应的位数,高位移出(舍弃),低位的空位补零。

  当左移的运算数是int 类型时,每移动1位它的第31位就要被移出并且丢弃;

  当左移的运算数是long 类型时,每移动1位它的第63位就要被移出并且丢弃。

  当左移的运算数是byte 和short类型时,将自动把这些类型扩大为 int 型。

  • 数学意义:

    在数字没有溢出的前提下,对于正数和负数,左移一位都相当于乘以2的1次方,左移n位就相当于乘以2的n次方。 3 << 2,其实就是3*2的二次方

右移运算符(>>)

  • 规则:

    按二进制形式把所有的数字向右移动对应的位数,低位移出(舍弃),高位的空位补符号位,即正数补零,负数补1   当右移的运算数是byte 和short类型时,将自动把这些类型扩大为 int 型。

  例如,如果要移走的值为负数,每一次右移都在左边补1,如果要移走的值为正数,每一次右移都在左边补0,这叫做符号位扩展 (保留符号位)(sign extension ),在进行右移

  操作时用来保持负数的符号。

  • 数学意义:除2

无符号右移(>>>)

直接在高位补0,低位删除。

        int i=-7;
        System.out.println(Integer.toBinaryString(i));
        System.out.println(Integer.toBinaryString(i>>>2));
        System.out.println(Integer.toBinaryString(i>>2));
        
        运行结果:
        11111111111111111111111111111001
        111111111111111111111111111110
        11111111111111111111111111111110

从以上运行结果可以看出>>> 和>>的区别

例子:

hashmap源代码中,计算key的hashcode方法如下

/**
     * Computes key.hashCode() and spreads (XORs) higher bits of hash
     * to lower.  Because the table uses power-of-two masking, sets of
     * hashes that vary only in bits above the current mask will
     * always collide. (Among known examples are sets of Float keys
     * holding consecutive whole numbers in small tables.)  So we
     * apply a transform that spreads the impact of higher bits
     * downward. There is a tradeoff between speed, utility, and
     * quality of bit-spreading. Because many common sets of hashes
     * are already reasonably distributed (so don't benefit from
     * spreading), and because we use trees to handle large sets of
     * collisions in bins, we just XOR some shifted bits in the
     * cheapest possible way to reduce systematic lossage, as well as
     * to incorporate impact of the highest bits that would otherwise
     * never be used in index calculations because of table bounds.
     */
    static final int hash(Object key) {
        int h;
        return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
    }

key的hashcode无符号右移了16位,然后与key的hashcode异或位运算。这种方法高效的让hashcode的高位和低位进行了一次异或运算。据说可以产生更加随机的hashcode,但是我并看不出来为什么,如果有大神做过测试或者知道其中的原理可以留言。 但是可以知道的是,这种运算符在源代码中很实用。