The Java Tutorials have been written for JDK 8.Java教程是为JDK 8编写的。Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.本页中描述的示例和实践没有利用后续版本中引入的改进,并且可能使用不再可用的技术。See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.有关Java SE 9及其后续版本中更新的语言特性的摘要,请参阅Java语言更改。
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.有关所有JDK版本的新功能、增强功能以及已删除或不推荐的选项的信息,请参阅JDK发行说明。
The Java programming language also provides operators that perform bitwise and bit shift operations on integral types.Java编程语言还提供了对整数类型执行按位和位移位操作的运算符。The operators discussed in this section are less commonly used.本节中讨论的运算符不太常用。Therefore, their coverage is brief; the intent is to simply make you aware that these operators exist.因此,他们的报道是简短的;其目的只是让您意识到这些操作符的存在。
The unary bitwise complement operator "一元逐位补码运算符“~
" inverts a bit pattern; it can be applied to any of the integral types, making every "0" a "1" and every "1" a "0".~
”反转位模式;它可以应用于任何整数类型,使每个“0”都成为“1”,每个“1”都成为“0”。For example, a 例如,一个byte
contains 8 bits; applying this operator to a value whose bit pattern is "00000000" would change its pattern to "11111111".byte
包含8位;将此运算符应用于位模式为“00000000”的值会将其模式更改为“11111111”。
The signed left shift operator "带符号的左移位运算符“<<
" shifts a bit pattern to the left, and the signed right shift operator ">>
" shifts a bit pattern to the right.<<
”将位模式向左移位,而带符号的右移位运算符“>>
”将位模式向右移动。The bit pattern is given by the left-hand operand, and the number of positions to shift by the right-hand operand.位模式由左侧操作数给出,位置数由右侧操作数移位。The unsigned right shift operator "无符号右移运算符“>>>
" shifts a zero into the leftmost position, while the leftmost position after ">>"
depends on sign extension.>>>
”将零移到最左边的位置,而将最左边的位置移到“>>
”取决于符号扩展。
The bitwise 按位&
operator performs a bitwise AND operation.&
运算符执行按位AND运算。
The bitwise 按位^
operator performs a bitwise exclusive OR operation.^
运算符执行按位异或运算。
The bitwise 按位|
operator performs a bitwise inclusive OR operation.|
运算符执行按位包含或运算。
The following program, 以下程序BitDemo
, uses the bitwise AND operator to print the number "2" to standard output.BitDemo
使用按位AND运算符将数字“2”打印到标准输出。
class BitDemo { public static void main(String[] args) { int bitmask = 0x000F; int val = 0x2222; // prints "2" System.out.println(val & bitmask); } }