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

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 =111
and2 =1
or1 =111
or2 =1
not =111
xor1 =111
xor2 =1
and = 1
andresult =1
or = 7
orresutl =111
not = -8
notresult =11111111111111111111111111111000
xor = 6
xorresult =110
最大:2147483647
最小:-2147483648
最大:1111111111111111111111111111111
最大:31
最小:10000000000000000000000000000000
最小:32

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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值