Java中的取模、取余和位移

1.取模 (a mod b)

        System.out.println("10    mod    3 = " + Math.floorMod(10, 3));
        System.out.println("10    mod (-3) = " + Math.floorMod(10, -3));
        System.out.println("(-10) mod    3 = " + Math.floorMod(-10, 3));
        System.out.println("(-10) mod (-3) = " + Math.floorMod(-10, -3));

结果


取模运算

结论

取模运算结果的符号与b的符号一致。

2.取余(a % b)

        System.out.println("10    %    3 = " + (10 % 3));
        System.out.println("10    % (-3) = " + (10 % (-3)));
        System.out.println("(-10) %    3 = " + ((-10) % 3));
        System.out.println("(-10) % (-3) = " + ((-10) % (-3)));

结果


取余

结论

取余运算结果的符号与a的符号一致。

3.左移(a << b)

        // -----------integer--------------
        System.out.println("10    <<    3 = " + (10 << 3));
        System.out.println("10    << (-3) = " + (10 << (-3)));
        System.out.println("10    <<   29 = " + (10 << 29));
        System.out.println("(-10) <<    3 = " + ((-10) << 3));
        System.out.println("(-10) << (-3) = " + ((-10) << (-3)));
        System.out.println("(-10) <<   29 = " + ((-10) << 29));
        // ----------- long --------------
        System.out.println("10L    <<    3 = " + (10L << 3));
        System.out.println("10L    << (-3) = " + (10L << (-3)));
        System.out.println("10L    <<   61 = " + (10L << (-3)));
        System.out.println("(-10L) <<    3 = " + ((-10L) << 3));
        System.out.println("(-10L) << (-3) = " + ((-10L) << (-3)));
        System.out.println("(-10L) <<   61 = " + ((-10L) << (-3)));

结果


左移

结论

a扩大相应倍数,且结果符号保持不变右侧补0,当b<0时,取(b mod 32)或(b mod 64)。

4.右移(a >> b)

        // -----------integer--------------
        System.out.println("10    >>    3 = " + (10 >> 3));
        System.out.println("10    >> (-3) = " + (10 >> (-3)));
        System.out.println("10    >>   29 = " + (10 >> 29));
        System.out.println("(-10) >>    3 = " + ((-10) >> 3));
        System.out.println("(-10) >> (-3) = " + ((-10) >> (-3)));
        System.out.println("(-10) >>   29 = " + ((-10) >> 29));
        // ----------- long --------------
        System.out.println("10L    >>    3 = " + (10L >> 3));
        System.out.println("10L    >> (-3) = " + (10L >> (-3)));
        System.out.println("10L    >>   61 = " + (10L >> (-3)));
        System.out.println("(-10L) >>    3 = " + ((-10L) >> 3));
        System.out.println("(-10L) >> (-3) = " + ((-10L) >> (-3)));
        System.out.println("(-10L) >>   61 = " + ((-10L) >> (-3)));

结果


右移

结论

a缩小相应倍数,且结果符号保持不变左侧补0,当b<0时,取(b mod 32)或(b mod 64)。

5.无符号右移(a >>> b)

        // -----------integer--------------
        System.out.println("10    >>>    3 = " + (10 >>> 3));
        System.out.println("10    >>> (-3) = " + (10 >>> (-3)));
        System.out.println("10    >>>   29 = " + (10 >>> 29));
        System.out.println("(-10) >>>    3 = " + ((-10) >>> 3));
        System.out.println("(-10) >>> (-3) = " + ((-10) >>> (-3)));
        System.out.println("(-10) >>>   29 = " + ((-10) >>> 29));
        // ----------- long --------------
        System.out.println("10L    >>>    3 = " + (10L >>> 3));
        System.out.println("10L    >>> (-3) = " + (10L >>> (-3)));
        System.out.println("10L    >>>   61 = " + (10L >>> (-3)));
        System.out.println("(-10L) >>>    3 = " + ((-10L) >>> 3));
        System.out.println("(-10L) >>> (-3) = " + ((-10L) >>> (-3)));
        System.out.println("(-10L) >>>   61 = " + ((-10L) >>> (-3)));

结果


无符号右移

结论

a缩小相应倍数,左侧所有位补0,当b<0时,取(b mod 32)或(b mod 64)。

6.其他整数的位移(char、byte、short)

        System.out.println("(char)10    << (-3) = " + ((char)10 << (-3)));
        System.out.println("(byte)10    << (-3) = " + ((byte)10 << (-3)));
        System.out.println("(short)10    << (-3) = " + ((short)10 << (-3)));
        System.out.println("10    << (-3) = " + (10 << (-3)));
        System.out.println("10    <<   29 = " + (10 << 29));

结果


其他整数位移

结论

其他整数(char、byte、short)位移时,先转化为int,然后再按照int的位移规则进行位移,并且其结果是int类型。

7.浮点数位移(float、double)

浮点数(float、double)不支持位移操作。

你可能感兴趣的:(Java中的取模、取余和位移)