二进制打印工具

该工具可以将一个数的以二进制形式打印出来。支持分隔,支持去除前端多余0。可用来调试代码,和学习计算机底层实现。

/**
 * Created by zhuguohui on 2018/12/14.
 */
public class BinaryPrintUtil {
    private static final long[] arr = new long[64];

    static {
        arr[0] = 0x8000000000000000L;
        for (int i = 1; i < arr.length; i++) {
            arr[i] = arr[i - 1] >>> 1;
        }
    }

    /**
     * 打印a的二进制表示
     *
     * @param a               要打印的数字
     * @param removeFrontZero 是否移除前端多余的0
     * @param space           间隔多少位空一格 -1 表示没有间隔,最大值为32
     * @return
     */
    public static String show(long a, boolean removeFrontZero, int space) {
        return show(a, 0, removeFrontZero, space);
    }

    /**
     * 打印a的二进制表示
     *
     * @param a               要打印的数字
     * @param removeFrontZero 是否移除前端多余的0
     * @param space           间隔多少位空一格 -1 表示没有间隔,最大值为32
     * @return
     */
    public static String show(int a, boolean removeFrontZero, int space) {
        return show(a, 32, removeFrontZero, space);
    }

    /**
     * 打印a的二进制表示
     *
     * @param a               要打印的数字
     * @param removeFrontZero 是否移除前端多余的0
     * @param space           间隔多少位空一格 -1 表示没有间隔,最大值为32
     * @return
     */
    public static String show(short a, boolean removeFrontZero, int space) {
        return show(a, 48, removeFrontZero, space);
    }

    /**
     * 打印a的二进制表示
     *
     * @param a               要打印的数字
     * @param removeFrontZero 是否移除前端多余的0
     * @param space           间隔多少位空一格 -1 表示没有间隔,最大值为32
     * @return
     */
    public static String show(byte a, boolean removeFrontZero, int space) {
        return show(a, 56, removeFrontZero, space);
    }

    /**
     * 打印a的二进制表示
     *
     * @param a
     * @param removeFrontZero 是否移除前端多余的0
     * @param space           间隔多少位空一格 -1 表示没有间隔,最大值为32
     * @return
     */
    public static String show(long a, int beginIndex, boolean removeFrontZero, int space) {
        //打印a的二进制数据
        StringBuilder builder = new StringBuilder();
        if (space > 32) {
            space = -1;
        }
        for (int i = beginIndex; i < arr.length; i++) {
            if ((a & arr[i]) == arr[i]) {
                builder.append("1");
            } else {
                builder.append("0");
            }
            if (space != -1 && i != 0 && (i + 1) % space == 0) {
                builder.append(" ");
            }
        }
        String result = builder.toString();
        if (removeFrontZero) {
            char[] s = builder.toString().toCharArray();
            int subStringBeginIndex = 0;
            for (int i = 0; i < s.length; i++) {
                if (s[i] != '1') {
                    continue;
                }
                subStringBeginIndex = i;
                if (space != -1) {
                    //找到了第一个非0数,开始向前搜索第一个空格
                    for (int j = subStringBeginIndex; j >= 0; j--) {
                        if (s[j] == ' ') {
                            subStringBeginIndex = j;
                            break;
                        }
                    }
                }

            }
            result = result.substring(subStringBeginIndex);
        }
        return result;
    }

使用效果
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_22706515/article/details/85060345