[ Pobierz całość w formacie PDF ]
.Because representing infinite numbers is a big problem for computers, division or modulus operations by zero result in an error.To be more specific, a runtime exception is thrown.You'll learn a lot more about exceptions in Chapter 16, “Exception Handling.”The bitwise AND, OR, and XOR operators (&, |, and ^) all act on the individual bits of an integer.These operators sometimes are useful when an integer is being used as a bit field.An example of this is when an integer is used to represent a group of binary flags.An int is capable of representing up to 32 different flags, because it is stored in 32 bits.Listing 13.5 contains the program Bitwise, which shows how to use the binary bitwise integer operators.lListing 13.5.The Bitwise class.lclass Bitwise { public static void main (String args[]) { int x = 5, y = 6; System.out.println(“x = “ + x); System.out.println(“y = “ + y); System.out.println(“x & y = “ + (x & y)); System.out.println(“x | y = “ + (x | y)); System.out.println(“x ^ y = “ + (x ^ y)); }}The output of running Bitwise follows:x = 5y = 6x & y = 4x | y = 7x ^ y = 3To understand this output, you must first understand the binary equivalents of each decimal number.In Bitwise, the variables x and y are set to 5 and 6, which correspond to the binary numbers 0101 and 0110.The bitwise AND operation compares each bit of each number to see if they are the same.It then sets the resulting bit to 1 if both bits being compared are 1, and 0 otherwise.The result of the bitwise AND operation on these two numbers is 0100 in binary, or decimal 4.The same logic is used for both of the other operators, except that the rules for comparing the bits are different.The bitwise OR operator sets the resulting bit to 1 if either of the bits being compared is 1.For these numbers, the result is 0111 binary, or 7 decimal.Finally, the bitwise XOR operator sets resulting bits to 1 if exactly one of the bits being compared is 1, and 0 otherwise.For these numbers, the result is 0011 binary, or 3 decimal.The left-shift, right-shift, and zero-fill-right-shift operators (<<, >>, and >>>) shift the individual bits of an integer by a specified integer amount.The following are some examples of how these operators are used:x << 3;y >> 7;z >>> 2;In the first example, the individual bits of the integer variable x are shifted to the left three places.In the second example, the bits of y are shifted to the right seven places.Finally, the third example shows z being shifted to the right two places, with zeros shifted into the two leftmost places.To see the shift operators in a real program, check out Shift in Listing 13.6.lListing 13.6.The Shift class.lclass Shift { public static void main (String args[]) { int x = 7; System.out.println(“x = “ + x); System.out.println(“x >> 2 = “ + (x >> 2)); System.out.println(“x << 1 = “ + (x << 1)); System.out.println(“x >>> 1 = “ + (x >>> 1)); }}The output of Shift follows:x = 7x >> 2 = 1x << 1 = 14x >>> 1 = 3The number being shifted in this case is the decimal 7, which is represented in binary as 0111.The first right-shift operation shifts the bits two places to the right, resulting in the binary number 0001, or decimal 1.The next operation, a left shift, shifts the bits one place to the left, resulting in the binary number 1110, or decimal 14.Finally, the last operation is a zero-fill right shift, which shifts the bits 1 place to the right, resulting in the binary number 0011, or decimal 3.Pretty simple, huh? And you probably thought it was difficult working with integers at the bit level!Based on these examples, you may be wondering what the difference is between the right-shift (>>) and zero-fill-right-shift operators (>>>).The right-shift operator appears to shift zeros into the leftmost bits, just like the zero-fill-right-shift operator, right? Well, when dealing with positive numbers, there is no difference between the two operators; they both shift zeros into the upper bits of a number.The difference arises when you start shifting negative numbers.Remember that negative numbers have the high-order bit set to 1.The right-shift operator preserves the high-order bit and effectively shifts the lower 31 bits to the right.This behavior yields results for negative numbers similar to those for positive numbers.That is, -8 shifted right by one will result in -4.The zero-fill-right-shift operator, on the other hand, shifts zeros into all the upper bits, including the high-order bit.When this shifting is applied to negative numbers, the high-order bit becomes 0 and the number becomes positive.RelationalThe last group of integer operators is the relational operators, which all operate on integers but return a type boolean.Table 13.3 lists the relational integer operators.Table 13.3.The relational integer operators.DescriptionOperatorLess Than<Greater Than>Less Than Or Equal To<=Greater Than Or Equal To>=Equal To==Not Equal To!=These operators all perform comparisons between integers.Listing 13.7 contains the Relational program, which demonstrates the use of the relational operators with integers.lListing 13.7.The Relational class.lclass Relational { public static void main (String args[]) { int x = 7, y = 11, z = 11; System.out.println(“x = “ + x); System.out.println(“y = “ + y); System.out.println(“z = “ + z); System.out.println(“x < y = “ + (x < y)); System.out.println(“x > z = “ + (x > z)); System.out.println(“y <= z = “ + (y <= z)); System.out.println(“x >= y = “ + (x >= y)); System.out.println(“y == z = “ + (y == z)); System.out
[ Pobierz całość w formacie PDF ]