java 里的逻辑运算符,与(&)或(|)非(~)和异或(^)

来源:互联网 发布:大智慧软件 编辑:程序博客网 时间:2024/05/23 23:41
package tablejava;public class AndOr2 {    public static void main(String[] args) {     int and1=7;     int and2=1;     int or1=7;     int or2=1;     int not=7;     int xor1=7;     int xor2=1;     System.out.println("and1 ="+Integer.toBinaryString(and1));     System.out.println("and2 ="+Integer.toBinaryString(and2));     System.out.println("or1 ="+Integer.toBinaryString(or1));     System.out.println("or2 ="+Integer.toBinaryString(or2));     System.out.println("not ="+Integer.toBinaryString(not));     System.out.println("xor1 ="+Integer.toBinaryString(xor1));     System.out.println("xor2 ="+Integer.toBinaryString(xor2));     int andresult=and1&and2;     System.out.println("and = "+andresult);     System.out.println("andresult ="+Integer.toBinaryString(andresult));     int orresutl=or1|or2;     System.out.println("or = "+orresutl);     System.out.println("orresutl ="+Integer.toBinaryString(orresutl));     int notresult=~not;     System.out.println("not = "+notresult);     System.out.println("notresult ="+Integer.toBinaryString(notresult));     int xorresult=xor1^xor2;     System.out.println("xor = "+xorresult);     System.out.println("xorresult ="+Integer.toBinaryString(xorresult));     System.out.println("最大:" + Integer.MAX_VALUE);     System.out.println("最小:" + Integer.MIN_VALUE);     System.out.println("最大:" + Integer.toBinaryString(Integer.MAX_VALUE));     String str1=Integer.toBinaryString(Integer.MAX_VALUE);     System.out.println("最大:" + str1.length());     System.out.println("最小:" + Integer.toBinaryString(Integer.MIN_VALUE));     String str2=Integer.toBinaryString(Integer.MIN_VALUE);     System.out.println("最小:" + str2.length());     /*      * int的取值范围为(-2147483648~2147483647),占用4个字节(-2的31次方到2的31次方-1)       *       * */    }}
and1 =111and2 =1or1 =111or2 =1not =111xor1 =111xor2 =1and = 1andresult =1or = 7orresutl =111not = -8notresult =11111111111111111111111111111000xor = 6xorresult =110最大:2147483647最小:-2147483648最大:1111111111111111111111111111111最大:31最小:10000000000000000000000000000000最小:32

在&和|运算中,如果两边是表达式则需要计算两边的值,
在&&和||运算中,则左边为假或者真就不再计算右边的值。

阅读全文
0 0
原创粉丝点击