位运算在算法中的部分应用

示例 1: 奇偶性判断

public class BitwiseExample {  
    public static void main(String[] args) {  
        int number = 5;  
        if ((number & 1) == 1) {  
            System.out.println(number + " 是奇数");  
        } else {  
            System.out.println(number + " 是偶数");  
        }  
    }  
}

示例 2: 交换两个变量的值(不使用临时变量)

public class BitwiseSwap {  
    public static void main(String[] args) {  
        int a = 5, b = 10;  
        a = a ^ b;  
        b = a ^ b; // 此时b = (a^b)^b = a^(b^b) = a^0 = a,恢复a的原值  
        a = a ^ b; // 此时a = (a^b)^a = b^(a^a) = b^0 = b,a变为b的值  
        System.out.println("a = " + a + ", b = " + b);  
    }  
}

示例 3: 快速幂运算

public class FastPower {  
    public static int fastPower(int base, int exponent) {  
        int result = 1;  
        while (exponent > 0) {  
            if ((exponent & 1) == 1) { // 如果当前位是1,则乘以base  
                result *= base;  
            }  
            exponent >>= 1; // 右移一位,相当于exponent除以2  
            base *= base; // base自乘,为下一次可能的乘法做准备  
        }  
        return result;  
    }  
  
    public static void main(String[] args) {  
        int base = 2;  
        int exponent = 10;  
        System.out.println(base + " 的 " + exponent + " 次方是 " + fastPower(base, exponent));  
    }  
}

示例 4: 简单的位掩码操作

public class BitMask {  
    public static void main(String[] args) {  
        int flags = 0; // 假设用于存储一系列布尔标志的位掩码  
  
        // 设置标志位  
        flags |= 1; // 假设第0位表示某个标志  
        flags |= (1 << 2); // 假设第2位表示另一个标志  
  
        // 检查标志位  
        if ((flags & 1) != 0) {  
            System.out.println("第0位标志已设置");  
        }  
        if ((flags & (1 << 2)) != 0) {  
            System.out.println("第2位标志已设置");  
        }  
  
        // 清除标志位  
        flags &= ~(1 << 2); // 清除第2位标志  
  
        // 再次检查  
        if ((flags & (1 << 2)) == 0) {  
            System.out.println("第2位标志已清除");  
        }  
    }  
}

示例5:二进制转换示例

public class BinaryConversion {  
    public static String toBinary(int number) {  
        if (number == 0) {  
            return "0";  
        }  
  
        StringBuilder binary = new StringBuilder();  
        while (number > 0) {  
            binary.insert(0, number % 2); // 插入当前的最低位  
            number /= 2; // 移除已处理的最低位  
        }  
        return binary.toString();  
    }  
  
    public static void main(String[] args) {  
        int number = 10;  
        System.out.println(number + " 的二进制表示为: " + toBinary(number));  
    }  
}

示例6:异或运算的应用

public class XORCipher {  
    private static final char KEY = 'K'; // 简单的密钥字符  
  
    // 使用异或运算加密字符串  
    public static String encrypt(String str) {  
        char[] chars = str.toCharArray();  
        for (int i = 0; i < chars.length; i++) {  
            chars[i] = (char) (chars[i] ^ KEY);  
        }  
        return new String(chars);  
    }  
  
    // 使用相同的异或运算解密字符串  
    public static String decrypt(String str) {  
        return encrypt(str); // 由于异或运算的自反性,加密和解密可以使用相同的函数  
    }  
  
    public static void main(String[] args) {  
        String original = "Hello, World!";  
        String encrypted = encrypt(original);  
        System.out.println("Encrypted: " + encrypted); // 输出将是一串乱码  
        String decrypted = decrypt(encrypted);  
        System.out.println("Decrypted: " + decrypted); // 输出应与原始字符串相同  
    }  
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值