栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java位运算

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java位运算

Java位运算 1、按位取反 ~
public class TestBitOperation {
    public static void main(String[] args) {
        //~n:	按位取反,0 -> 1, 1 -> 0
        System.out.println(Integer.toBinaryString(1));
        //1
        System.out.println(Integer.toBinaryString(~1));
        //11111111111111111111111111111110
        System.out.println(Integer.toBinaryString(5));
        //101
        System.out.println(Integer.toBinaryString(~5));
        //11111111111111111111111111111010
    }
}
2、按位与 &
public class TestBitOperation {
    public static void main(String[] args) {
        //& 按位与 可以把1看做true,把0看成false,只有都为true的时候,结果才为true
        //也就是说  二进制情况下,只有在该位上都为1的时候结果才为1,否则为0
        System.out.println(Integer.toBinaryString(6));//110
        System.out.println(Integer.toBinaryString(3));//11
        System.out.println(Integer.toBinaryString(6&3));//10
    }
}
3、按位或 |
public class TestBitOperation {
    public static void main(String[] args) {
        //	| 按位或,在该位上只要有一个1,结果就为1,否则为0
        System.out.println(Integer.toBinaryString(6));//110
        System.out.println(Integer.toBinaryString(2));//10
        System.out.println(Integer.toBinaryString(6|2));//110
    }
}
4、按位异或 ^
public class TestBitOperation {
    public static void main(String[] args) {
        //    ^   按位异或,在该位上的值相同时,结果为0,不同时为1
        System.out.println(Integer.toBinaryString(6));//110
        System.out.println(Integer.toBinaryString(2));//10
        System.out.println(Integer.toBinaryString(6^2));//100
    }
}
5、按位左位移 <<
public class TestBitOperation {
    public static void main(String[] args) {
        //  <<  按位左位移,不管该数是正数还是负数,右边都补0。
        //  如果超出了该数据类型的位数,则从头开始
        System.out.println(Integer.toBinaryString(1));//1
        System.out.println(Integer.toBinaryString(1<<(Integer.SIZE-1)));
        //10000000000000000000000000000000
        System.out.println(Integer.toBinaryString(1<<(Integer.SIZE)));//1
        System.out.println(Integer.toBinaryString(1<<(Integer.SIZE+1)));//10
        System.out.println(1<<(Integer.SIZE+1));//2
        
        System.out.println(Integer.toBinaryString(-1));   						//11111111111111111111111111111111
        System.out.println(Integer.toBinaryString(-1<<3));
        //11111111111111111111111111111000
        System.out.println(Integer.toBinaryString(-1<<(Integer.SIZE-1)));
        //10000000000000000000000000000000
    }
}
6、按位右位移 >>
public class TestBitOperation {
    public static void main(String[] args) {
        //  >>  按位右位移,是正数,左边补0;是负数,左边补1
        System.out.println(Integer.toBinaryString(-30));
        //11111111111111111111111111100010
        System.out.println(Integer.toBinaryString(-30>>1));
        //11111111111111111111111111110001
        System.out.println(Integer.toBinaryString(16));//10000
        System.out.println(Integer.toBinaryString(16>>3));//10
    }
}
7、无符号右位移 >>>
public class TestBitOperation {
    public static void main(String[] args) {
        //  >>  按位右位移,是正数,左边补0;是负数,左边补1
        System.out.println(Integer.toBinaryString(-30));
        //11111111111111111111111111111111
        System.out.println(Integer.toBinaryString(-30>>>1));//左边补0
        //1111111111111111111111111110001
        System.out.println(Integer.toBinaryString(16));//10000
        System.out.println(Integer.toBinaryString(16>>>3));//10
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/345265.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号