Date:2018/11/25
- 逻辑运算符 & | ^ !
&: 与 有false则false
| : 或 有true则true
^: 异或 相同为false ,不同为true
! :非 取反
(对于整数来说,上面的这些运算符可以当作位运算符,比如10 | 6 = 14,而后者不能对整数进行操作)
短路与&&和与& 的区别(同短路或||和或| 的区别):
1、结果一样
2、&&具有短路的效果,如果左边是false,右边就不执行了
int x = 4;
int y = 3;
System.out.println((++x == 4) && (++y == 3));
System.out.println(x);
System.out.println(y);
输出: x = 5 ; y = 3(右边没有执行)
- 位运算符:& | ^ ~
System.out.println(6 & 3); 输出 2
System.out.println(6 | 3); 输出 7
System.out.println(6 ^ 3); 输出 5
System.out.println(~6); 输出 -7
解析:
位运算符都是将数值变成二进制来算的,
&与(有0为0) : 0110 & 0011 = 0010 即 2
|或(有1为1) : 0110 | 0011 = 0111 即 7
^异或(相同为0,不同为1) : 0110 ^ 0011 = 0101 即 5
~与 :~0110
00000000 00000000 00000000 00000110 6
11111111 11111111 11111111 11111001 ~6(补码)
11111111 11111111 11111111 11111000 反码
10000000 00000000 00000000 00000111 -7(符号位是按位取反了,此处不用改!)
面试题:不用第三方变量交换两个变量的值
1、用第三方变量:
int x = 4;
int y = 3;
int c;
c = y;
y = x;
x = c;
2、方法一:
int x = 4;
int y = 3;
x = x + y;
y = x - y; 此时y = x
x = x - y;
3、方法二:利用 x ^ y ^ y = x ,和同一个数异或两次值不变
int x = 4;
int y = 3;
x = x ^ y;
y = x ^ y; 此时y = x
x = x ^ y;
左移右移运算符 <<、>>:
<< 左移,最高为丢弃,右边补0
>> 右移,最高为是0,补0,最高为是1,补1
对于正数来说,左移几位就是乘以2的几次幂,右移就是除以2的几次幂
负数有点小麻烦,要变成补码才能运算,然后再变成原码,左移和正数一样左移几位就是乘以2的几次幂,右移需要看具体操作
不管左移右移,都不改变符号
负数左移-12 << 1:
10000000 00000000 00000000 00001100 -12
11111111 11111111 11111111 11110011 反码
11111111 11111111 11111111 11110100 补码
11111111 11111111 11111111 11101000 左移一位
11111111 11111111 11111111 11100111 减一反码
10000000 00000000 00000000 00011000 -24 原码
注:正数右移到最后等于0
负数右移-12 >> 3:若是正数12 >> 3 = 1;
10000000 00000000 00000000 00001100 -12
11111111 11111111 11111111 11110011 反码
11111111 11111111 11111111 11110100 补码
11111111 11111111 11111111 11111110 右移3位
11111111 11111111 11111111 11111101 减一反码
10000000 00000000 00000000 00000010 -2 原码
注:负数右移到最后等于-1
11111111 11111111 11111111 11111111 右移N位
11111111 11111111 11111111 11111110 减一反码
10000000 00000000 00000000 00000001 -1 原码
无符号右移 >>>:
和有符号右移>> 不一样,高位全补0;
三目运算符:expr1 ? expr2 : expr 3 :
expr1 为true ,选择 expr2 ,为false,选择expr 3
Q:找出变量中的最大值
int q = 10;
int w = 20;
int e = 30;
int temp = (q > w) ? q : w ;
int max = (temp > e) ? temp : e;
System.out.println(max);
最后做几个小练习综合以上:
import java.util.Scanner;
public class LongTest {
private static Scanner sc;
public static void main(String[] args) {
sc = new Scanner(System.in);
int x = sc.nextInt();
int y = sc.nextInt();
int z = sc.nextInt();
int temp = (x > y ? x : y);
int max = (temp > z ? temp : z);
System.out.println( "和:" + (x + y));
System.out.println("2最大值: "+ (x > y ? x : y));
System.out.println("是否相等:" + (x == y ? true : false));
System.out.println("3max:" + max);
}
}
关于switch语句:
直接上一个switch和if实现一个简单的功能:判定季节
最主要的就是switch里面有个case穿透,在执行了这个case里的内容后,还会继续往下执行,所以每个case最好都要绑定一个break,
另外,default关键字可加可不加,给switch做另外的补充用:
import java.util.Scanner;
public class LongTest {
private static Scanner sc;
public static void main(String[] args) {
sc = new Scanner(System.in);
int x = sc.nextInt();
if (x == 3 || x == 4 || x == 5 ) {
System.out.println("春季");
}else if(x == 6 || x == 7 || x == 8) {
System.out.println("夏季");
}else if(x == 9 || x == 10 || x == 11) {
System.out.println("qq");
}else if(x == 12 || x == 1 || x == 2) {
System.out.println("dd");
}
switch(x) {
case 3:
{System.out.println("春季");break;}
case 4:
{System.out.println("春季");break;}
case 5:
{System.out.println("春季");break;}
case 6:
{System.out.println("夏季");break;}
case 7:
{System.out.println("夏季");break;}
case 8:
{System.out.println("夏季");break;}
case 9:
{System.out.println("q季");break;}
case 10:
{System.out.println("q季");break;}
case 11:
{System.out.println("q季");break;}
case 12:
{System.out.println("dd");break;}
case 1:
{System.out.println("dd");break;}
case 2:
{System.out.println("dd");break;}
default:
{System.out.println("输入要在1~12之间!");}*/
}
}