java之运算符(20190131)
1. 算数运算符
+
-
*
/
% //取余
++ //自增
-- //自减
2. 逻辑运算符
& //与
&& //短路与
| //或
|| //短路或
! //非
^ //异或;两边相同为false,两边不同为true。
3. 位运算符
<< //左移(左移后,最低位补零)
示例:
00000000 00000000 00000000 00000110 int a = 6;
00000000 00000000 00000000 00001100 a << 1 = 12;
---------------------------------------------------
>> //右移(原来最高位是什么,右移后,最高位就补什么)
示例:
00000000 00000000 00000000 00000110 int a = 6;
00000000 00000000 00000000 00000011 a >> 1 = 3;
---------------------------------------------------
>>> //无符号右移(无论原最高位是什么,右移后,最高位补零)
示例:
11111111 11111111 11111111 11111110 int a = -2;
01111111 11111111 11111111 11111111 a >>> 1 = 2147483647;
---------------------------------------------------
& //与位运算
示例:
00000000 00000000 00000000 00000110 int a = 6;
00000000 00000000 00000000 00000011 int b = 3;
00000000 00000000 00000000 00000010 a & b = 2;
---------------------------------------------------
| //或位运算
示例:
00000000 00000000 00000000 00000110 int a = 6;
00000000 00000000 00000000 00000011 int b = 3;
00000000 00000000 00000000 00000111 a | b = 7;
---------------------------------------------------
^ //异或位运算
示例:
00000000 00000000 00000000 00000110 int a = 6;
00000000 00000000 00000000 00000011 int b = 3;
00000000 00000000 00000000 00000101 a ^ b = 5;
6 ^ 3 ^ 3 = 6
一个数异或另一个数两次,结果还是这个数。
---------------------------------------------------
~ //反码
示例:
00000000 00000000 00000000 00000110 int a = 6;
11111111 11111111 11111111 11111001 ~ a = -7;
4. 三元运算符
int x = 1, y;
y = x > 1 ? 100 : 200;
网友评论