原码补码反码与位操作

今天搜索互联网那个引擎了解了原码补码反码的相关概念
原码是最直观的编码方式,用最高位表示符号(0为正,1为负),其余位表示数值的绝对值
正数的反码与原码相同;负数的反码是对其原码的数值位取反(符号位不变)
正数的补码与原码相同;负数的补码是其反码加1
Java中的整数采用补码表示法
public class Demo{
public static void main(String[] args) {
// 定义测试用的正数和负数
int y = 13; // 二进制: 00001101
int b = -13; // 二进制: 11110011 (补码表示)

        // 打印原始数值的二进制表示
        System.out.println("原始数值:");
        printBinary("正数13", y);
        printBinary("负数-13", b);
        System.out.println();

        // 位与操作 (&)
        System.out.println("位与操作 (&):");
        int andResult = y & b;
        printOperation("13 & -13", andResult, y, b, "&");
        System.out.println();

        // 位或操作 (|)
        System.out.println("位或操作 (|):");
        int orResult = y | b;
        printOperation("13 | -13", orResult, y, b, "|");
        System.out.println();

        // 位异或操作 (^)
        System.out.println("位异或操作 (^):");
        int xorResult = y ^ b;
        printOperation("13 ^ -13", xorResult, y, b, "^");
        System.out.println();

        // 位非操作 (~)
        System.out.println("位非操作 (~):");
        int noty = ~y;
        int notb = ~b;
        printOperation("~13", noty, y);
        printOperation("~-13", notb, b);
        System.out.println();

        // 左移操作 (<<)
        System.out.println("左移操作 (<<):");
        int leftShiftPos = y << 2;
        int leftShiftNeg = b << 2;
        printShiftOperation("13 << 2", leftShiftPos, y, "<<", 2);
        printShiftOperation("-13 << 2", leftShiftNeg, b, "<<", 2);
        System.out.println();

        // 算术右移操作 (>>)
        System.out.println("算术右移操作 (>>):");
        int rightShiftPos = y >> 2;
        int rightShiftNeg = b >> 2;
        printShiftOperation("13 >> 2", rightShiftPos, y, ">>", 2);
        printShiftOperation("-13 >> 2", rightShiftNeg, b, ">>", 2);
        System.out.println();

        // 逻辑右移操作 (>>>)
        System.out.println("逻辑右移操作 (>>>):");
        int unsignedRightShiftPos = y >>> 2;
        int unsignedRightShiftNeg = b >>> 2;
        printShiftOperation("13 >>> 2", unsignedRightShiftPos, y, ">>>", 2);
        printShiftOperation("-13 >>> 2", unsignedRightShiftNeg, b, ">>>", 2);
    }

    // 打印二进制表示的辅助方法
    public static void printBinary(String label, int num) {
        String binary = String.format("%32s", Integer.toBinaryString(num)).replace(' ', '0');
        System.out.println(label + " (十进制:" + num + "): " + binary);
    }

    // 打印位操作结果的辅助方法
    public static void printOperation(String operation, int result, int operand1, int operand2, String operator) {
        System.out.println(operation + " = " + result);
        System.out.print("二进制运算: ");
        printBinaryShort(operand1);
        System.out.print(" " + operator + " ");
        printBinaryShort(operand2);
        System.out.print(" = ");
        printBinaryShort(result);
        System.out.println("\n");
    }

    // 打印位操作结果的辅助方法
    public static void printOperation(String operation, int result, int operand) {
        System.out.println(operation + " = " + result);
        System.out.print("二进制运算: " + operation.charAt(0) + " ");
        printBinaryShort(operand);
        System.out.print(" = ");
        printBinaryShort(result);
        System.out.println("\n");
    }

    // 打印移位操作结果的辅助方法
    public static void printShiftOperation(String operation, int result, int operand, String shiftOp, int shift) {
        System.out.println(operation + " = " + result);
        System.out.print("二进制运算: ");
        printBinaryShort(operand);
        System.out.print(" " + shiftOp + " " + shift + " = ");
        printBinaryShort(result);
        System.out.println("\n");
    }
    // 简短格式打印二进制
    public static void printBinaryShort(int num) {
        String binary = Integer.toBinaryString(num);
        if (binary.length() > 8) {
            binary = binary.substring(binary.length() - 8);
        }
        System.out.print(binary);
    }

}
手工计算结果-13的逻辑右移和算数右移与java表示结果不一样
经过搜索了解手工8位简算与java32位补码运算完全计算结果不一样,java的结算结果是正确的
我学会了要在分析位运算时,必须考虑数据类型的完整位数(int为32位),简单的8位模型会导致理解偏差。

posted @ 2025-09-22 20:04  clo3o  阅读(13)  评论(0)    收藏  举报