移位运算

public class BitwiseShiftOperators {

    private static int lhs;
    private static int rhs;
    private static int result;
    private static final int count = 10;

    public static void main(String[] args) {
        lhs = 1;
        rhs = 2;
        leftShift(lhs, rhs);

        System.out.println("----------");

        lhs = -1024;
        rhs = 3;
        rightShift(lhs, rhs);

        System.out.println("----------");

        lhs = 1024;
        rhs = 3;
        logicalRightShift(lhs, rhs);
    }

    private static void leftShift(int lhs, int rhs) {
        
        System.out.println("左移");

        loopPrintExpression(lhs, " << ", " * ", rhs);        
    }

    private static void rightShift(int lhs, int rhs) {

        System.out.println("右移");

        loopPrintExpression(lhs, " >> ", " / ", rhs);
    }

    private static void logicalRightShift(int lhs, int rhs) {
        
        System.out.println("逻辑右移");

        if (lhs > 0) {
            loopPrintExpression(lhs, " >>> ", " / ", rhs);
        } else {
            loopPrintExpression(lhs, " >>> ", "   ", rhs);
        }
    }

    private static void printPow(int lhs, String operator, int rhs) {
        if (operator.equals("   ")) {
            return;
        }
        System.out.println(lhs + operator + "Math.pow(2, " + rhs + ")");
        compute(lhs, operator, rhs);
        System.out.println(lhs + operator + pow(2, rhs) + " = " + result);
    }

    private static void loopPrintExpression(int lhs, String operator, String otherOperator, int rhs) {
        System.out.println(lhs);
        printBinary(lhs);
        for (int i = 0; i < count; i++) {
            printExpression(lhs, operator, rhs);
            printPow(lhs, otherOperator, rhs);
            printBinary(result);
            lhs = result;
        }
    }
 
    private static void printExpression(int lhs, String operator, int rhs) {
        compute(lhs, operator, rhs);
        System.out.println(lhs + operator + rhs + " = " + result);
    }

    private static void compute(int lhs, String operator, int rhs) {
        switch (operator) {
            case " * ":
                result = lhs * pow(2, rhs);
                break;
            case " / ":
                result = lhs / pow(2, rhs);
                break;
            case " << ":
                result = lhs << rhs;
                break;

            case " >> ":
                result = lhs >> rhs;
                break;

            case " >>> ":
                result = lhs >>> rhs;
                break;

            default:
                break;
        }
    }

    private static void printBinary(int lhs) {
        System.out.println("binary: " + Integer.toBinaryString(lhs));
    }
    
    private static int pow(int a, int b) {
        if (b == 1) {
            return a;
        } else {
            return a * pow(a, b - 1);
        }
        // return (int) Math.pow(a, b);
    }
}
posted @ 2021-10-19 21:28  clipboard  阅读(60)  评论(0编辑  收藏  举报