二进制操作

本文深入探讨了二进制位操作的各种运算,包括或运算、与运算、异或运算、取反运算、左移和右移等。通过具体的代码示例,详细解释了每种运算的原理和结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

二进制操作

public class BitOperationTest {

    /**
     * 或运算
     */
    private static void testHuo(){
        /*
         * 真真为真,真假为真 假假为假
         * 0000 0001
         * 0000 0101
         * ----------
         * 0000 0101
         * ----------
         * 5           结果
         */
        System.out.println("或:"+(1|5));
    }

    /**
     * 与运算
     */
    private static void testYu(){
        /*
         * 真真为真 其他为假
         * 0000 0001
         * 0000 0101
         * ----------
         * 0000 0001
         * -----------
         * 1          结果
         */
        System.out.println("与:"+(1&5));
    }

    /**
     * 异或运算
     */
    private static void testYiHuo(){
        /*
         * 真真为假 假假为假 真假为真
         * 0000 0001
         * 0000 0101
         * ----------
         * 0000 0100
         * ----------------
         * 4              结果
         */
        System.out.println("异或"+(1^5));
    }

    /**
     * 取反运算
     */
    private static void testQuFan(){
        /*
         * 真真为假 假假为假 真假为真
         *
         * 0000 0101
         * ----------
         * 1111 1010
         * ----------
         * 0000 0101          补码+1
         * ----------------
         *  -6               结果
         */
        System.out.println(""+(~5));
    }

    /**
     * 无符号左移
     */
    private static void testLeftMove(){
        /*
        * 0000 0101
        * ---------
        * 0001 0100 左移
        * -------------
        * 20        结果
        * */
        System.out.println(5 << 2);
    }

    /**
     * 无符号右移
     */
    private static void testRightMove(){
        /*
         * 0000 0101
         * ---------
         * 0000 0001 右移
         * -------------
         * 1        结果
         * */
        System.out.println(5 >> 2);
    }

    /**
     * 带符号左移
     */
    private static void testLeftMoveWithFh(){
        /*
         * 1111 1011
         * ---------
         * 1110 1100    左移
         * -------------
         * 0001 0011 补码 +1
         * -------------------
         * 0001 0100
         * ---------------
         * -20        结果
         * */
        System.out.println(-5 << 2);
    }

    /**
     * 带符号右移
     */
    private static void testRightMoveWithFh(){
        /*
         * 1111 1011
         * ---------
         * 1111 1110 右移
         * -------------
         * -2        结果
         * */
        System.out.println(-5 >> 2);
    }

    public static void main(String[] args) {
        testHuo();
        testYu();
        testYiHuo();
        testQuFan();
        testLeftMove();
        testRightMove();
        testRightMoveWithFh();
        testLeftMoveWithFh();
        System.out.println(256 >>> 2);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值