java 位移运算符

import org.junit.Test;

/**
 * 1)<< : 左移运算符
 * 2)>> : 右移运算符 (测试正数)
 * 3)>> : 右移运算符 (测试负数)
 * 4)>>> : 无符号右移 (测试正数)
 * 5)>>> : 无符号右移 (测试负数)
 */
public class WeiYiTest {

    /**
     * << : 左移运算符
     * 测试数字:101
     */
    @Test
    public void test1() {
        System.out.println(Integer.toBinaryString(101)); // 1100101

        System.out.println(101 << 8); // 25856
        System.out.println(Integer.toBinaryString(101 << 8)); // 110010100000000

        /*
         * 左移8位逻辑
         *            01100101    // 原数据101
         *   01100101             // 向左位移8位,右侧空余的位置用0补全
         *   <----<---<----<-
         *   01100101 00000000    // 位移后得到的数据,25856
         */
        System.out.println(Integer.parseInt("0110010100000000", 2)); // 25856

        // ==================================================================== //

        System.out.println(101 << 16); // 6619136
        System.out.println(Integer.toBinaryString(101 << 16)); // 1100101 00000000 00000000

        /*
         * 左移16位逻辑
         *                    01100101    // 原数据101
         *   01100101                     // 向左位移16位,右侧空余的位置用0补全
         *   <----<---<----<----<----<
         *   01100101 0000000 00000000    // 位移后得到的数据,6619136
         */

        System.out.println(Integer.parseInt("011001010000000000000000", 2)); // 6619136
    }

    /**
     * >> : 右移运算符
     * ----------------
     * 测试正数:1010001001
     */
    @Test
    public void test2_1() {
        System.out.println(Integer.toBinaryString(1010001001)); // 111100001100110110010001101001

        System.out.println(1010001001 >> 8); // 3945316
        System.out.println(Integer.toBinaryString(1010001001 >> 8)); // 1111000011001101100100

        /*
         * 右移8位逻辑
         *   00111100 00110011 01100100 01101001
         *            00111100 00110011 01100100    // 向右位移8位,左侧空余的位置用0补全
         *   ---->--->---->----->---->---->---->
         *   00000000 00111100 00110011 01100100    // 位移后得到的数据,3945316
         */

        System.out.println(Integer.parseInt("001111000011001101100100", 2)); // 3945316

        // ==================================================================== //

        System.out.println(1010001001 >> 16); // 15411
        System.out.println(Integer.toBinaryString(1010001001 >> 16)); // 11110000110011

        /*
         * 右移16位逻辑
         *   00111100 00110011 01100100 01101001
         *                     00111100 00110011    // 向右位移16位,左侧空余的位置用0补全
         *   ---->--->---->----->---->---->---->
         *   00000000 00000000 00111100 00110011    // 位移后得到的数据,15411
         */

        System.out.println(Integer.parseInt("0011110000110011", 2)); // 15411
    }

    /**
     * >> : 右移运算符
     * 测试负数:-1010001001
     * --------------------------------
     * 位移后,还是负数,符号位没有改变
     */
    @Test
    public void test2_2() {
        System.out.println(Integer.toBinaryString(-1010001001)); // 11000011110011001001101110010111

        System.out.println(-1010001001 >> 8); // -3945317
        System.out.println(Integer.toBinaryString(-1010001001 >> 8)); // 11111111110000111100110010011011

        /*
         * 右移8位逻辑
         *   11000011 11001100 10011011 10010111
         *            11000011 11001100 10011011    // 向右位移8位,左侧空余的位置用1补全
         *   ---->--->---->----->---->---->---->
         *   11111111 11000011 11001100 10011011    // 位移后得到的数据,-3945317
         */

        // ==================================================================== //

        System.out.println(-1010001001 >> 16); // -15412
        System.out.println(Integer.toBinaryString(-1010001001 >> 16)); // 11111111111111111100001111001100

        /*
         * 右移16位逻辑
         *   11000011 11001100 10011011 10010111
         *                     11000011 11001100    // 向右位移16位,左侧空余的位置用1补全
         *   ---->--->---->----->---->---->---->
         *   11111111 11111111 11000011 11001100    // 位移后得到的数据,-15412
         */
    }

    /**
     * >>> : 无符号右移
     * 测试正数:1010001001
     */
    @Test
    public void test3_1() {
        System.out.println(Integer.toBinaryString(1010001001)); // 111100001100110110010001101001

        System.out.println(1010001001 >>> 8); // 3945316
        System.out.println(Integer.toBinaryString(1010001001 >>> 8)); // 1111000011001101100100

        /*
         * 右移8位逻辑
         *   00111100 00110011 01100100 01101001
         *            00111100 00110011 01100100    // 向右位移8位,左侧空余的位置用0补全
         *   ---->--->---->----->---->---->---->
         *   00000000 00111100 00110011 01100100    // 位移后得到的数据,3945316
         */

        System.out.println(Integer.parseInt("001111000011001101100100", 2)); // 3945316

        // ==================================================================== //

        System.out.println(1010001001 >>> 16); // 15411
        System.out.println(Integer.toBinaryString(1010001001 >>> 16)); // 11110000110011

        /*
         * 右移16位逻辑
         *   00111100 00110011 01100100 01101001
         *                     00111100 00110011    // 向右位移16位,左侧空余的位置用0补全
         *   ---->--->---->----->---->---->---->
         *   00000000 00000000 00111100 00110011    // 位移后得到的数据,15411
         */

        System.out.println(Integer.parseInt("0011110000110011", 2)); // 15411
    }

    /**
     * >>> : 无符号右移
     * 测试负数:-1010001001
     * -----------------------------
     * 位移后,负数变正数了
     */
    @Test
    public void test3_2() {
        System.out.println(Integer.toBinaryString(-1010001001)); // 11000011110011001001101110010111

        System.out.println(-1010001001 >>> 8); // 12831899
        System.out.println(Integer.toBinaryString(-1010001001 >>> 8)); // 110000111100110010011011

        /*
         * 右移8位逻辑
         *   11000011 11001100 10011011 10010111
         *            11000011 11001100 10011011    // 向右位移8位,左侧空余的位置用0补全
         *   ---->--->---->----->---->---->---->
         *   00000000 11000011 11001100 10011011    // 位移后得到的数据,12831899
         */

        System.out.println(Integer.parseInt("110000111100110010011011", 2)); // 12831899

        // ==================================================================== //

        System.out.println(-1010001001 >>> 16); // 50124
        System.out.println(Integer.toBinaryString(-1010001001 >>> 16)); // 1100001111001100

        /*
         * 右移16位逻辑
         *   11000011 11001100 10011011 10010111
         *                     11000011 11001100    // 向右位移16位,左侧空余的位置用0补全
         *   ---->--->---->----->---->---->---->
         *   00000000 00000000 11000011 11001100    // 位移后得到的数据,50124
         */

        System.out.println(Integer.parseInt("1100001111001100", 2)); // 50124
    }

}
原文地址:https://www.cnblogs.com/zj0208/p/7988565.html