Bitwise Logical Operators(位运算符)由于在一般的日常开发当中很少涉及,所以在《Thinking in java》,《Core Java 2》等Java书籍中只是略有提及,一笔带过。
也没找到一本参考书对其有详细描述,兴趣所致,在网上搜索了许多资料。终于大致了解了其原理。
位运算符包括:~,|,&,^
~ the NOT Operator (非运算符)
| the OR Operator (或运算符)
& the AND Operator (与运算符)
^ the XOR Operator (异或运算符)
[size=small][b]~ the NOT Operator(非运算符)[/b][/size]
以下是《Thinking in java》中的描述:
The bitwise NOT (~, also called the ones complement operator) is a unary operator; it takes only one argument. (All other bitwise operators are binary operators.)
非运算符是一个一元运算符,它只需要一个参数。(其它的位运算符都是二元运算符)
Bitwise NOT produces the opposite of the input bit—a one if the input bit is zero, a zero if the input bit is one.
非运算符会对输入(input)进行位取反运算,0变成1,1变成0。(假设输入(input)是一个十进制数,则先取其二进制表示,再进行位取反运算)
示例1
为何 ~0 = -1?
0用二进制表示为:00000000000000000000000000000000(更多关于二进制与十进制相互转换的信息,见[url]http://denverj.iteye.com/blog/736637[/url])
取反就是: 11111111111111111111111111111111
可以看出这是一个负数,因为在二进制中,最左位为1表示负数。要找出它的实际值,我们采用二进制补码算法。(更多二进制补码的信息,见[url]http://denverj.iteye.com/blog/736583[/url])
第一步,每一个二进制位都取相反值,0变成1,1变成0。比如,11111111111111111111111111111111的相反值就是00000000000000000000000000000000。
第二步,将上一步得到的值加1。00000000000000000000000000000000就变成00000000000000000000000000000001。
00000000000000000000000000000001代表1。
所以11111111111111111111111111111111代表二进制的-1。
为何 ~11 = -12?
11用二进制表示为:00000000000000000000000000001011
取反就是: 11111111111111111111111111110100
可以看出这是一个负数,因为在二进制中,最左位为1表示负数。要找出它的实际值,我们采用二进制补码算法。
第一步,每一个二进制位都取相反值,0变成1,1变成0。比如,11111111111111111111111111110100的相反值就是00000000000000000000000000001011。
第二步,将上一步得到的值加1。00000000000000000000000000001011就变成00000000000000000000000000001100。
00000000000000000000000000001100代表12。(更多关于二进制与十进制相互转换的信息,见[url]http://denverj.iteye.com/blog/736637[/url])
所以11111111111111111111111111110100代表二进制的-12。
[size=small][b]| the OR Operator (或运算符)[/b][/size]
以下是《Thinking in java》中的描述:
The bitwise OR operator (|) produces a one in the output bit if either input bit is a one and produces a zero only if both input bits are zero.
如果输入(input)的两位中只要有一个为1,则或运算符会返回1。只有两个都是0,它才返回0。(假设输入(input)是二个十进制数,则先取其二进制表示,再进行具体运算)
示例2
为何 11|10 = 11?
11用二进制表示为:00000000000000000000000000001011
10用二进制表示为:00000000000000000000000000001010
执行非运算符对每一位进行运算,得到00000000000000000000000000001011
00000000000000000000000000001011表示11。
[size=small][b]& the AND Operator (与运算符)[/b][/size]
以下是《Thinking in java》中的描述:
The bitwise AND operator (&) produces a one in the output bit if both input bits are one, otherwise it produces a zero.
如果输入(input)位都是1,则与运算符返回1。否则为0。(假设输入(input)是二个十进制数,则先取其二进制表示,再进行具体运算)
示例3
为何 11&10 = 10?
11用二进制表示为:00000000000000000000000000001011
10用二进制表示为:00000000000000000000000000001010
执行非运算符对每一位进行运算,得到00000000000000000000000000001010
00000000000000000000000000001010表示10。
[size=small][b]^ the XOR Operator (异或运算符)[/b][/size]
以下是《Thinking in java》中的描述:
The bitwise EXCLUSIVE OR, or XOR (^), produces a one in the output bit if one or the other input bit is a one, but not both.
如果输入(input)的两位当中有且只有一个为1,则异或运算符会返回1。
示例4
为何 11^10 = 1?
11用二进制表示为:00000000000000000000000000001011
10用二进制表示为:00000000000000000000000000001010
执行异或运算符对每一位进行运算,得到00000000000000000000000000000001
00000000000000000000000000000001表示1。
参考资料链接:[url]http://blog.youkuaiyun.com/yz394777014/archive/2009/07/28/4387728.aspx[/url]
<Thinking in Java 3rd Edtion> Bruce Eckel
也没找到一本参考书对其有详细描述,兴趣所致,在网上搜索了许多资料。终于大致了解了其原理。
位运算符包括:~,|,&,^
~ the NOT Operator (非运算符)
| the OR Operator (或运算符)
& the AND Operator (与运算符)
^ the XOR Operator (异或运算符)
[size=small][b]~ the NOT Operator(非运算符)[/b][/size]
以下是《Thinking in java》中的描述:
The bitwise NOT (~, also called the ones complement operator) is a unary operator; it takes only one argument. (All other bitwise operators are binary operators.)
非运算符是一个一元运算符,它只需要一个参数。(其它的位运算符都是二元运算符)
Bitwise NOT produces the opposite of the input bit—a one if the input bit is zero, a zero if the input bit is one.
非运算符会对输入(input)进行位取反运算,0变成1,1变成0。(假设输入(input)是一个十进制数,则先取其二进制表示,再进行位取反运算)
示例1
class TheNotOperator {
public static void main(String args[]) {
// invert 0
System.out.println("~ 0 = " + ~0);
// invert 11
System.out.println("~ 11 = " + ~11);
}
}
为何 ~0 = -1?
0用二进制表示为:00000000000000000000000000000000(更多关于二进制与十进制相互转换的信息,见[url]http://denverj.iteye.com/blog/736637[/url])
取反就是: 11111111111111111111111111111111
可以看出这是一个负数,因为在二进制中,最左位为1表示负数。要找出它的实际值,我们采用二进制补码算法。(更多二进制补码的信息,见[url]http://denverj.iteye.com/blog/736583[/url])
第一步,每一个二进制位都取相反值,0变成1,1变成0。比如,11111111111111111111111111111111的相反值就是00000000000000000000000000000000。
第二步,将上一步得到的值加1。00000000000000000000000000000000就变成00000000000000000000000000000001。
00000000000000000000000000000001代表1。
所以11111111111111111111111111111111代表二进制的-1。
为何 ~11 = -12?
11用二进制表示为:00000000000000000000000000001011
取反就是: 11111111111111111111111111110100
可以看出这是一个负数,因为在二进制中,最左位为1表示负数。要找出它的实际值,我们采用二进制补码算法。
第一步,每一个二进制位都取相反值,0变成1,1变成0。比如,11111111111111111111111111110100的相反值就是00000000000000000000000000001011。
第二步,将上一步得到的值加1。00000000000000000000000000001011就变成00000000000000000000000000001100。
00000000000000000000000000001100代表12。(更多关于二进制与十进制相互转换的信息,见[url]http://denverj.iteye.com/blog/736637[/url])
所以11111111111111111111111111110100代表二进制的-12。
[size=small][b]| the OR Operator (或运算符)[/b][/size]
以下是《Thinking in java》中的描述:
The bitwise OR operator (|) produces a one in the output bit if either input bit is a one and produces a zero only if both input bits are zero.
如果输入(input)的两位中只要有一个为1,则或运算符会返回1。只有两个都是0,它才返回0。(假设输入(input)是二个十进制数,则先取其二进制表示,再进行具体运算)
示例2
public class TheOrOperator {
public static void main(String args[]) {
// apply the | operator
int x = 11 | 10;
System.out.println("11|10 = " + x);
}
}
为何 11|10 = 11?
11用二进制表示为:00000000000000000000000000001011
10用二进制表示为:00000000000000000000000000001010
执行非运算符对每一位进行运算,得到00000000000000000000000000001011
00000000000000000000000000001011表示11。
[size=small][b]& the AND Operator (与运算符)[/b][/size]
以下是《Thinking in java》中的描述:
The bitwise AND operator (&) produces a one in the output bit if both input bits are one, otherwise it produces a zero.
如果输入(input)位都是1,则与运算符返回1。否则为0。(假设输入(input)是二个十进制数,则先取其二进制表示,再进行具体运算)
示例3
public class TheAndOperator {
public static void main(String args[]) {
// apply the & operator
int x = 11 & 10;
System.out.println("11&10 = " + x);
}
}
为何 11&10 = 10?
11用二进制表示为:00000000000000000000000000001011
10用二进制表示为:00000000000000000000000000001010
执行非运算符对每一位进行运算,得到00000000000000000000000000001010
00000000000000000000000000001010表示10。
[size=small][b]^ the XOR Operator (异或运算符)[/b][/size]
以下是《Thinking in java》中的描述:
The bitwise EXCLUSIVE OR, or XOR (^), produces a one in the output bit if one or the other input bit is a one, but not both.
如果输入(input)的两位当中有且只有一个为1,则异或运算符会返回1。
示例4
public class TheXOROperator {
public static void main(String args[]) {
// apply the ^ operator
int x = 11 ^ 10;
System.out.println("11^10 = " + x);
}
}
为何 11^10 = 1?
11用二进制表示为:00000000000000000000000000001011
10用二进制表示为:00000000000000000000000000001010
执行异或运算符对每一位进行运算,得到00000000000000000000000000000001
00000000000000000000000000000001表示1。
参考资料链接:[url]http://blog.youkuaiyun.com/yz394777014/archive/2009/07/28/4387728.aspx[/url]
<Thinking in Java 3rd Edtion> Bruce Eckel