java常用位操作

public class TestIndex {
    /**
     * 数组越界
     * 这个方法是java源码中常用的一个数组越界的判断检测
     * 顺便复习一下位操作
     */
    public static int read(byte[] b, int off, int len) throws Exception {
        // parameter check
        int result = off | len | (off + len) |(b.length - (off+len));
        if (result < 0) {
            System.out.println(result);
        } else if (len == 0) {
            return 0;
        }
        return result;
    }

    /**
     * 将一个高位在前的字节数组转为int
     * @param num
     * @return
     */
    public static byte[] intToBytes(int num){
        byte[] bytes = new byte[4];
        bytes[0] = (byte) (num >>> 24);
        bytes[1] = (byte) (num >>> 16);
        bytes[2] = (byte) (num >>> 8);
        bytes[3] = (byte) (num >>> 0);
        return bytes;
    }

    /**
     * 将一个高位在前的字节数组转为int
     * @param bytes
     * @return
     */
    public static int byteToInt(byte[] bytes){
        int _1 = bytes[0] << 24;
        int _2 = bytes[1] << 16;
        int _3 = bytes[2] << 8;
        int _4 = bytes[3] << 0;
        return _1 | _2 | _3 | _4;
    }

    /**
     * 显示一个byte数组
     * @param bytes
     */
    public static void showByte(byte[] bytes){
        for (byte b : bytes) {
            System.out.print(byteToBit(b) +" ");
        }
        System.out.println();
    }

    /** 
     * 将byte转换为一个长度为8的byte数组,数组每个值代表bit 
     */  
    public static byte[] getBooleanArray(byte b) {  
        byte[] array = new byte[8];  
        for (int i = 7; i >= 0; i--) {  
            array[i] = (byte)(b & 1);  
            b = (byte) (b >> 1);  
        }  
        return array;  
    }  
    /** 
     * 把byte转为字符串的bit 
     */  
    public static String byteToBit(byte b) {  
        return ""  
                + (byte) ((b >> 7) & 0x1) + (byte) ((b >> 6) & 0x1)  
                + (byte) ((b >> 5) & 0x1) + (byte) ((b >> 4) & 0x1)  
                + (byte) ((b >> 3) & 0x1) + (byte) ((b >> 2) & 0x1)  
                + (byte) ((b >> 1) & 0x1) + (byte) ((b >> 0) & 0x1);  
    }  

    public static void main(String[] args) {
        int num = 100;
        byte[] bytes = intToBytes(num);
        showByte(bytes);
        num = byteToInt(bytes);
        System.out.println(num);
    }
}
Java 中,位与运算符用 `&` 表示,它会对两个操作数的对应二进制位进行逐位比较,只有当两个对应位都为 1 时,结果的该位才为 1,否则为 0。以下是一些 Java常用的位与运算场景: ### 判断一个数是否为 2 的幂 判断一个数是否为 2 的幂,可以使用 `(val & -val) == val` 来实现。如果一个数是 2 的幂,那么它的二进制表示中只有一位是 1,其余位都是 0。取负操作会将该数的二进制表示按位取反后加 1,这样原数和其负值进行位与运算的结果就是原数本身。示例代码如下: ```java private static boolean isPowerOfTwo(int val) { return (val & -val) == val; } ``` ### 取模运算(除数为 2 的 n 次幂) 当除数为 2 的 n 次幂时,可以使用位与运算替代取模运算,`X & (2^n - 1)` 等同于 `X % (2^n)`。示例代码如下: ```java public class Main { public static void main(String[] args) { int dividend = 10; int divisor = 8; // 8 是 2 的 3 次幂,即 2^3 int resultBitwise = dividend & (divisor - 1); System.out.println("位与运算替代取模的结果: " + resultBitwise); } } ``` ### 提取特定位 可以使用位与运算来提取一个数的特定位。例如,要提取一个整数的最低 4 位,可以将该数与二进制数 `0000 0000 0000 0000 0000 0000 0000 1111`(用十六进制表示为 `0x0F`)进行位与运算。示例代码如下: ```java public class Main { public static void main(String[] args) { int num = 255; // 二进制表示: 0000 0000 0000 0000 0000 0000 1111 1111 int result = num & 0x0F; // 提取最低 4 位 System.out.println("提取最低 4 位的结果: " + result); } } ``` ### 关闭特定位 可以使用位与运算将一个数的特定位设置为 0。例如,要将一个整数的第 3 位(从右向左数,从 0 开始计数)设置为 0,可以将该数与二进制数 `1111 1111 1111 1111 1111 1111 1111 1011`(用十六进制表示为 `0xFB`)进行位与运算。示例代码如下: ```java public class Main { public static void main(String[] args) { int num = 15; // 二进制表示: 0000 0000 0000 0000 0000 0000 0000 1111 int result = num & 0xFB; // 将第 3 位设置为 0 System.out.println("将第 3 位设置为 0 的结果: " + result); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值