zoukankan      html  css  js  c++  java
  • 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
        }
    
    }
  • 相关阅读:
    day39——多线程实例、多线程锁
    day38——多进程Manager、进程池
    day37——多进程锁、多进程共享内存
    day36——多进程多线程概念、多进程、多进程实例
    day35——memcache常用方法
    day34——memcached安装、memcached集群操作
    day33——hash类型操作、其他常用操作
    day25——NoSQL的字符串操作、list操作、set操作
    day24——NoSQL简介、redis服务搭建、redis连接池、redis管道
    Linux日常巡检脚本
  • 原文地址:https://www.cnblogs.com/zj0208/p/7988565.html
Copyright © 2011-2022 走看看