官术网_书友最值得收藏!

  • 雙語版Java程序設計
  • 何月順主編
  • 1009字
  • 2018-12-27 20:14:16

4.4 Bitwise and Shift Operators

The bitwise and shift operators are low-level operators that manipulate the individual bits that make up an integer value. The bitwise operators are most commonly used for testing and setting individual flag bits in a value. In order to understand their behavior, you must understand binary (base-2) numbers and the twos-complement format used to represent negative integers. You cannot use these operators with floating-point, boolean, array, or object operands. When used with boolean operands, the &, |, and ^ operators perform a different operation, as described in the previous section.

If either of the arguments to a bitwise operator is a long, the result is a long. Otherwise, the result is an int. If the left operand of a shift operator is a long, the result is a long; otherwise, the result is an int.

位運算和移位運算是底層的運算,可以實現(xiàn)將單個的二進制位組裝成整型。位運算符通常被用來測試和設置單個二進制位的值。如果位運算操作數(shù)是long型,其結果也是long型;否則,結果就是int型。

4.4.1 Bitwise Complement (~)

The unary ~ operator is known as the bitwise complement, or bitwise NOT, operator. It inverts each bit of its single operand, converting ones to zeros and zeros to ones. For example:

byte b = ~12; // ~00000110 ==> 11111001 or -13 decimal
flags = flags & ~f;

4.4.2 Bitwise AND (&)

This operator combines its two integer operands by performing a Boolean AND operation on their individual bits. The result has a bit set only if the corresponding bit is set in both operands. For example:

&運算符將參加運算的兩個整數(shù)操作數(shù)按二進制位進行布爾AND運算。如果相應的二進制位都為1,則該位的結果為1。

10 & 7 // 00001010 & 00000111 ==> 00000010 or 2
if ((flags & f) != 0)

When used with boolean operands, & is the infrequently used Boolean AND operator described earlier.

4.4.3 Bitwise OR ( | )

This operator combines its two integer operands by performing a Boolean OR operation on their individual bits. The result has a bit set if the corresponding bit is set in either or both of the operands. It has a zero bit only where both corresponding operand bits are zero. For example:

|運算符將參加運算的兩個整數(shù)操作數(shù)按二進制位進行布爾OR運算。如果相應的二進制位為1或者全都為1,則該位的結果為1。相應的二進制位全都為0,則該位的結果為0。

10 | 7 // 00001010 | 00000111 ==> 00001111 or 15
flags = flags | f;

When used with boolean operands, | is the infrequently used Boolean OR operator described earlier.

4.4.4 Bitwise XOR (^)

This operator combines its two integer operands by performing a Boolean XOR (Exclusive OR) operation on their individual bits. The result has a bit set if the corresponding bits in the two operands are different. If the corresponding operand bits are both ones or both zeros, the result bit is a zero. For example:

10 & 7 // 00001010 ^ 00000111 ==> 00001101 or 13

When used with boolean operands, ^ is the infrequently used Boolean XOR operator.

4.4.5 Left Shift (<<)

The << operator shifts the bits of the left operand left by the number of places specified by the right operand. High-order bits of the left operand are lost, and zero bits are shifted in from the right. Shifting an integer left by n places is equivalent to multiplying that number by 2n. For example:

<<運算符將它左邊的操作數(shù)的二進制數(shù)左移該運算符右邊操作數(shù)指定的位數(shù)。

10 << 1 // 00001010 << 1 = 00010100 = 20 = 10*2
7 << 3 // 00000111 << 3 = 00111000 = 56 = 7*8
-1 << 2 // 0xFFFFFFFF << 2 = 0xFFFFFFFC = -4 = -1*4

If the left operand is a long, the right operand should be between 0 and 63. Otherwise, the left operand is taken to be an int, and the right operand should be between 0 and 31.

4.4.6 Signed Right Shift (>>)

The >> operator shifts the bits of the left operand to the right by the number of places specified by the right operand. The low-order bits of the left operand are shifted away and are lost. The high-order bits shifted in are the same as the original high-order bit of the left operand. In other words, if the left operand is positive, zeros are shifted into the high-order bits. If the left operand is negative, ones are shifted in instead. This technique is known as sign extension; it is used to preserve the sign of the left operand. For example:

>>運算符將它左邊的操作數(shù)的二進制數(shù)右移該運算符右邊操作數(shù)指定的位數(shù)。如果左邊的這個操作數(shù)是正數(shù),移位后高位補0。如果左邊的這個操作數(shù)是負數(shù),移位后高位補1。這被稱為符號擴展,常用來保存左邊操作數(shù)的符號。

10 >> 1 // 00001010 >> 1 = 00000101 = 5 = 10/2
27 >> 3 // 00011011 >> 3 = 00000011 = 3 = 27/8
-50 >> 2 // 11001110 >> 2 = 11110011 = -13 != -50/4

If the left operand is positive and the right operand is n, the >> operator is the same as integer division by 2n.

4.4.7 Unsigned Right Shift (>>>)

This operator is like the >> operator, except that it always shifts zeros into the high-order bits of the result, regardless of the sign of the left-hand operand. This technique is called zero extension; it is appropriate when the left operand is being treated as an unsigned value (despite the fact that Java integer types are all signed). Examples:

-50 >>> 2 // 11001110 >>> 2 = 00110011 = 51
0xff >>> 4 // 11111111 >>> 4 = 00001111 = 15 = 255/16
主站蜘蛛池模板: 锡林浩特市| 曲水县| 伊春市| 伊金霍洛旗| 荃湾区| 铜鼓县| 伽师县| 静安区| 长沙市| 渝中区| 黔西县| 恩施市| 英德市| 安化县| 乐安县| 海宁市| 白山市| 冀州市| 和静县| 岑溪市| 浦城县| 兰溪市| 东宁县| 佛坪县| 兴山县| 台安县| 三明市| 正镶白旗| 东兴市| 巍山| 河池市| 翁牛特旗| 万山特区| 兴城市| 南阳市| 乌什县| 林口县| 治县。| 沙坪坝区| 象山县| 依安县|