public class bitTest{
public static void main(String[] args){
int n1=10;
int n2=5;
System.out.println("n1:"+n1+",n2:"+n2);
//方式一:定义临时变量
int temp = n1;
n1 = n2;
n2 = temp;
System.out.println("n1:"+n1+",n2:"+n2);
//方式二:好处:不用定义临时变量
//弊端:①相加操作可能超出存储范围②有局限性只适用于数值类型
n1 = n1+n2;
n2 = n1-n2;
n1 = n1-n2;
System.out.println("n1:"+n1+",n2:"+n2);
//方式三:使用位运算符(异或运算符的特性 m=(m^n)^n)
//实际上,方式一和方式二一样
n1 = n1^n2;
n2 = n1^n2;
n1 = n1^n2;
System.out.println("n1:"+n1+",n2:"+n2);
}
}
05-21
1298
08-28
1288
05-21
1408
04-20
5695