zoukankan      html  css  js  c++  java
  • 二进制、十进制转化工具

    /**
     * 二进制、十进制转化工具类
     */
    public class BinaryUtil {
        /**
         * description: 根据二进制数获取值  例如,输入1010,获取到的是[1,3]
         *
         * @param code:输入10101
         * @param digit        位数
         * @return
         */
        public static List<Integer> getArr(Integer code, Integer digit) {
            //补齐位数
            String newCode = completeDigit(code, digit);
            byte[] arrByte = newCode.getBytes();
            List<Integer> arrayList = new ArrayList<>();
            if (arrByte.length > 0) {
                for (int i = 0; i < arrByte.length; i++) {
                    //1对应的ascii为49,0为48
                    if (arrByte[i] == 49) {
                        arrayList.add(i + 1);
                    }
                }
            }
            return arrayList;
        }
    
    
        /**
         * 根据二进制 例如,输入[1,3,4],获取到的是1011
         *
         * @param arr
         * @param digit 位数
         * @return
         */
        public static Integer getBinary(List<Integer> arr, Integer digit) {
            int binary = 0;
            if (arr != null && arr.size() > 0) {
                int tempBinary = 0;
                StringBuilder builder = new StringBuilder("1");
                if (digit >= 1) {
                    for (int i = 1; i < digit; i++) {
                        builder.append("0");
                    }
                }
                //获取第一位对应的二进制数
                int decimalise = Integer.parseInt(builder.toString(), 2);
                for (int i = 0; i < arr.size(); i++) {
                    tempBinary = decimalise >> arr.get(i) - 1;//右移
                    binary = tempBinary | binary;//按位或
                }
            }
            return Integer.parseInt(Integer.toBinaryString(binary));
        }
    
    
        /**
         * 将输入的二进制补齐位数
         *
         * @param code
         * @param num  长度
         * @return
         */
        public static String completeDigit(Integer code, Integer num) {
            // 0-代表前面补充0  num-长度   d-代表参数为正数型
            return String.format("%0" + num + "d", code);
        }
    
    
        /**
         * 判断某天是否工作  例如 输入1100 ,总位数7 ,指定位数4 ,返回false
         *
         * @param code       例如 1011
         * @param totalDigit 总位数
         * @param digit      指定位数
         * @return
         */
        public static Boolean isExist(Integer code, Integer totalDigit, Integer digit) {
            String newCode = completeDigit(code, totalDigit);
            int target = Integer.parseInt(newCode.substring(digit - 1, digit));
            return target == 1;
        }
    
    
        /**
         * 改变指定的摸个某个数字  例如 输入1100 ,总位数7 ,指定位数4 ,目标数据0  返回101
         *
         * @param code       例如 1011
         * @param totalDigit 总位数
         * @param digit      指定位数
         * @param target      目标数据
         * @return
         */
        public static Integer changeNum(Integer code, Integer totalDigit, Integer digit, Integer target) {
            int binaryCode = Integer.parseInt(code.toString(),2);
            int temp = 0;
            if(target == 1){
                temp = binaryCode | (target << digit-1);//按位或
            }else if(target == 0){
                if (!getStatusType(binaryCode,digit)){
                    temp = binaryCode;
                    return  temp;
                }
                //10010 - 1向左移(index-1)
                temp = binaryCode - (1 << (digit - 1));
            }
            return temp;//111011
        }
    
    
        /**
         * 根据二进制 例如,输入[1,3,4],获取到的是binary
         *
         * @param arr
         * @param digit 位数
         * @return
         */
        public static Integer getDemical(List<Integer> arr, Integer digit) {
            int binary = 0;
            if (arr != null && arr.size() > 0) {
                int tempBinary = 0;
                StringBuilder builder = new StringBuilder("1");
                if (digit >= 1) {
                    for (int i = 1; i < digit; i++) {
                        builder.append("0");
                    }
                }
                //获取第一位对应的二进制数
                int decimalise = Integer.parseInt(builder.toString(), 2);
                for (Integer integer : arr) {
                    tempBinary = decimalise >> integer - 1;//右移
                    binary = tempBinary | binary;//按位或
                }
            }
            return binary;
        }
    
    
        private static boolean getStatusType(int num, int index) {
            return (num >> (index - 1) & 1) == 1;
        }
    
    
        /**
         * 测试方法
         *
         * @param arags
         */
        public static void main(String[] arags) {
            //测试:根据二进制数获取是星期几
            List<Integer> day = BinaryUtil.getArr(10101, 7);
    
            //System.out.println(isExist(101, 7, 3));
            System.out.println(changeNum(1101,  5,  2,1));
            //测试:根据星期数获取对应的二进制
            List<Integer> list = new ArrayList<Integer>();
            list.add(1);
            list.add(2);
            list.add(4);
            list.add(6);
            System.out.println(getDemical(list,7));
    
    //        System.out.println( BinaryToDecimalUtil.changeNum(0, 7, 5,1));
    
            Integer binaryDay = BinaryUtil.getBinary(list, 6);
            //System.out.println(binaryDay);
    
    
        }
    }
    随笔看心情
  • 相关阅读:
    LeetCode 42. Trapping Rain Water
    LeetCode 209. Minimum Size Subarray Sum
    LeetCode 50. Pow(x, n)
    LeetCode 80. Remove Duplicates from Sorted Array II
    Window10 激活
    Premiere 关键帧缩放
    AE 「酷酷的藤」特效字幕制作方法
    51Talk第一天 培训系列1
    Premiere 视频转场
    Premiere 暴徒生活Thug Life
  • 原文地址:https://www.cnblogs.com/stromgao/p/15791331.html
Copyright © 2011-2022 走看看