zoukankan      html  css  js  c++  java
  • java >> << .>>> 计算详解

    通过一天的学习,整理了下面的小例子,分析了整数位运算的具体步骤,帮助大家理解和运用位运算符

    package com.base;
    
    public class Calculate {
        
        /**
         * 1 int 的取值是31位,最高位表示0表示正数,1表示负数
         *     min    = 10000000000000000000000000000000 = -2147483648
         *     max    = 01111111111111111111111111111111 =  2147483647
         * 2 负数使用补码表示,计算方法为最高位=1,其余31位为正数取反后加1
         *  20 = 00000000000000000000000000010100 
         *  取反  11111111111111111111111111101011
         *                                     +1
         * -20 = 11111111111111111111111111101100 
         * @param args
         */
        public static void main(String[] args) {
            
            //------- 整数位移运算---------------------------
            testLeft(2,16);// 正数左移
            testLeft(-2,16);//负数左移
            testRight(20,3);// 正数右移 
            testRight(-20,5);// 负数右移
            testUnsignedRight(20,5);// 正数无符号右移
            testUnsignedRight(-20,5);// 负数无符号右移
            //二进制取值范围
            testIntegerRag();  //int
        }
    
        
    
        /**
         *  2 << 16
         *         2 = 00000000000000000000000000000010 
         *      << 16
         *             00000000000000100000000000000000
         *      = 131072
         *  
         *  -2 << 16
         *      -2= 11111111111111111111111111111110
         *      << 16
         *          11111111111111100000000000000000
         *        = -131072
         */
        private static void testLeft(int i,int n) {
            System.out.println(" ---------- "+i+" << "+n+" -----------------");
            System.out.println("before = "+Integer.toBinaryString(i));
            int a = i << n;
            System.out.println(" after = "+Integer.toBinaryString(a));
            System.out.println("result = "+a);
        }
        
        /**
         * 4 >> 1   -- 0100 >> 0010  正数右移
         *                          负数右移
         * -20 >> 4  11111111111111111111111111101100 
         *   - >>4   1111111111111111111111111110 
         *   - -1    1111111111111111111111111101
         *   - 取反   1000000000000000000000000010 = -2 
         *   
         * -20 >> 5  11111111111111111111111111101100 
         *   - >>5   111111111111111111111111111 
         *   - -1    111111111111111111111111110
         *   - 取反   100000000000000000000000001 = -1   
         * @param i
         * @param n
         */
        private static void testRight(int i,int n) {
            System.out.println(" ---------- "+i+" >> "+n+" -----------------");
            System.out.println("before = "+Integer.toBinaryString(i));
            int a = i >> n;
            System.out.println(" after = "+Integer.toBinaryString(a));
            System.out.println("result = "+a);
        }
        
        /**
         * 无符号位置
         * -20 >>> 5
         *  -20    =  11111111111111111111111111101100
         * >>> 5   =  111111111111111111111111111
         * 转十进制 =  134217727
         *  20 >>> 5
         *   20    =  00000000000000000000000000010100
         * >>> 5   =  00000000000000000000000000000000
         * 转十进制 =  0
         * @param i
         * @param n
         */
        private static void testUnsignedRight(int i,int n){
            System.out.println(" ---------- "+i+" >>> "+n+" -----------------");
            System.out.println("before = "+Integer.toBinaryString(i));
            int a = i >>> n;
            System.out.println(" after = "+Integer.toBinaryString(a));
            System.out.println("result = "+a);
        }
        
        /**
         * Integer 二进制取值范围 
         */
        private static void testIntegerRag() {
            System.out.println(" ---------- int -----------------");
            System.out.println(" "+Integer.toBinaryString(Integer.MIN_VALUE)+"="+Integer.MIN_VALUE);
            System.out.println("  "+Integer.toBinaryString(Integer.MAX_VALUE)+"= "+Integer.MAX_VALUE);
        }
    }
  • 相关阅读:
    IDEA小插件之快速修改Maven多模块的工程版本
    Java 8 Lambda实现原理分析
    怒学Java8系列一:Lambda表达式
    Redis有序集内部实现原理分析(二)
    Redis有序集内部实现原理分析
    JVM内存堆布局图解分析
    Redis内存回收:LRU算法
    垃圾收集器:引用计数算法
    Maven解读:项目依赖管理如何优化
    Maven解读:强大的依赖体系
  • 原文地址:https://www.cnblogs.com/caer/p/7196258.html
Copyright © 2011-2022 走看看