【Java】位运算基础知识
与运算($)或运算(|)取反异或运算(^)左移右移
学而不思则罔,思而不学则殆
位运算包括:与或非,异或,左移和右移
运算符总结
与(&)都为1才为1或(|)有1才为1非(~)取反异或(^)相同为0,不同为1左移(<<)普通左移和循环左移右移(>>)普通右移和循环右移
与运算($)
System
.out
.println("测试与(&)");
System
.out
.println("0 & 0 = " + (0 & 0));
System
.out
.println("0 & 1 = " + (0 & 1));
System
.out
.println("1 & 0 = " + (1 & 0));
System
.out
.println("1 & 1 = " + (1 & 1));
测试与
(&)
0 & 0 = 0
0 & 1 = 0
1 & 0 = 0
1 & 1 = 1
全为1为1,只要有一个为0则为0
或运算(|)
System
.out
.println("测试或(|)");
System
.out
.println("0 | 0 = " + (0 | 0));
System
.out
.println("0 | 1 = " + (0 | 1));
System
.out
.println("1 | 0 = " + (1 | 0));
System
.out
.println("1 | 1 = " + (1 | 1));
测试或
(|)
0 | 0 = 0
0 | 1 = 1
1 | 0 = 1
1 | 1 = 1
全为0为0,只要有一个为1则为1
取反
~1010 = 0101
~1101 = 0010
负数取反
System
.out
.println("~0 =" + Integer
.toBinaryString(~0));
System
.out
.println("~1 =" + Integer
.toBinaryString(~1));
System
.out
.println("~-1 =" + Integer
.toBinaryString(~-1));
测试取反
(~)
0 = 00000000000000000000000000000000
~0 = 11111111111111111111111111111111
1 = 00000000000000000000000000000001
~1 = 11111111111111111111111111111110
-1 = 11111111111111111111111111111111
~-1 = 00000000000000000000000000000000
计算机的机器码都是补数。了解更多细节请查看原码,反码,补码概念 【-1】的原码是1000000000000000000000000001 【-1】的反码是11111111111111111111111111111110 【-1】的补码是11111111111111111111111111111111
异或运算(^)
System
.out
.println("测试异或(^)");
System
.out
.println("0 ^ 0 = " + (0 ^ 0));
System
.out
.println("0 ^ 1 = " + (0 ^ 1));
System
.out
.println("1 ^ 0 = " + (1 ^ 0));
System
.out
.println("1 ^ 1 = " + (1 ^ 1));
测试异或
(^)
0 ^ 0 = 0
0 ^ 1 = 1
1 ^ 0 = 1
1 ^ 1 = 0
相同为0,不同为1
左移
System
.out
.println("测试左移(<<)");
int num
= 0b101;
System
.out
.println(Integer
.toBinaryString(num
) + " << 1 = " + Integer
.toBinaryString(num
<< 1));
System
.out
.println(Integer
.toBinaryString(num
) + " << 2 = " + Integer
.toBinaryString(num
<< 2));
System
.out
.println(Integer
.toBinaryString(num
) + " << 3 = " + Integer
.toBinaryString(num
<< 3));
System
.out
.println(Integer
.toBinaryString(num
) + " << 4 = " + Integer
.toBinaryString(num
<< 4));
num
= -1;
System
.out
.println(num
+ " = " + Integer
.toBinaryString(num
));
System
.out
.println(Integer
.toBinaryString(num
) + " << 1 = " + Integer
.toBinaryString((num
<< 1)));
System
.out
.println(Integer
.toBinaryString(num
) + " << 2 = " + Integer
.toBinaryString(num
<< 2));
System
.out
.println(Integer
.toBinaryString(num
) + " << 3 = " + Integer
.toBinaryString(num
<< 3));
System
.out
.println(Integer
.toBinaryString(num
) + " << 4 = " + Integer
.toBinaryString(num
<< 4));
测试左移
(<<)
5 = 101
101 << 1 = 1010
101 << 2 = 10100
101 << 3 = 101000
101 << 4 = 1010000
-1 = 11111111111111111111111111111111
11111111111111111111111111111111 << 1 = 11111111111111111111111111111110
11111111111111111111111111111111 << 2 = 11111111111111111111111111111100
11111111111111111111111111111111 << 3 = 11111111111111111111111111111000
11111111111111111111111111111111 << 4 = 11111111111111111111111111110000
左移低位补0
右移
System
.out
.println("测试右移(>>)");
int num
= 0b101101;
System
.out
.println(num
+ " = " + Integer
.toBinaryString(num
));
System
.out
.println(Integer
.toBinaryString(num
) + " >> 1 = " + Integer
.toBinaryString(num
>> 1));
System
.out
.println(Integer
.toBinaryString(num
) + " >> 2 = " + Integer
.toBinaryString(num
>> 2));
System
.out
.println(Integer
.toBinaryString(num
) + " >> 3 = " + Integer
.toBinaryString(num
>> 3));
System
.out
.println(Integer
.toBinaryString(num
) + " >> 4 = " + Integer
.toBinaryString(num
>> 4));
num
= -1;
System
.out
.println(num
+ " = " + Integer
.toBinaryString(num
));
System
.out
.println(Integer
.toBinaryString(num
) + " >> 1 = " + Integer
.toBinaryString((num
>> 1)));
System
.out
.println(Integer
.toBinaryString(num
) + " >> 2 = " + Integer
.toBinaryString(num
>> 2));
System
.out
.println(Integer
.toBinaryString(num
) + " >> 3 = " + Integer
.toBinaryString(num
>> 3));
System
.out
.println(Integer
.toBinaryString(num
) + " >> 4 = " + Integer
.toBinaryString(num
>> 4));
测试右移
(>>)
45 = 101101
101101 >> 1 = 10110
101101 >> 2 = 1011
101101 >> 3 = 101
101101 >> 4 = 10
-1 = 11111111111111111111111111111111
11111111111111111111111111111111 >> 1 = 11111111111111111111111111111111
11111111111111111111111111111111 >> 2 = 11111111111111111111111111111111
11111111111111111111111111111111 >> 3 = 11111111111111111111111111111111
11111111111111111111111111111111 >> 4 = 11111111111111111111111111111111
正数高位补0,负数高位补1