1.&& 短路与;或前是false则或后面的代码不再执行
int i = 3;
int j = 2;
boolean result = (i < j) && (++j == 3);
System.out.println(j);//j的值为2
2.
//利用位运算判断奇偶数
//2<<2 == 8;左移运算符
//利用位运算判断奇偶数
//2<<2 == 8
public class code01 {
public static void main(String[] args) {
int i = 8;
int j = 9;
System.out.println(i + "是:" + (((i & 1) == 0)?"偶数":"奇数"));
System.out.println(j + "是:" + (((j&1) == 1)?"奇数":"偶数"));
int m = 2;
System.out.println(m<<2);
}
}
3. 获取二进制位是1还是0
4.交换两个整数变量的值
public class code02 {
public static void main(String[] args) {
int m = 0b1010110;
//方法一
System.out.println(m + "的第五位上的二进制数是:" + (((m&(1<<4))>>4) == 0?"0":"1"));
//1
//方法二
System.out.println(m + "的第五位上的二进制数是:" + (((m>>4)&1) == 0?"0":"1"));
//1
int i = 10;
int j = 20;
i = i^j;
j = i^j;
i = i^j;
System.out.println("i:" + i + ", j:" + j);
//i:20, j:10
}
}