Chapter 20 Java Utilities Package and Bit (Geocities web hosting) Manipulation
Chapter 20 Java Utilities Package and Bit Manipulation 1175 0 through 5. Then simply shift this value by adding 1 to produce a number in the range from 1 through 6. The expression is as follows: Math.abs( r.nextInt() ) % 6 + 1 The calls r.nextFloat() r.nextDouble() generate uniformly distributed values in the range 0.0 <= x < 1.0. The call r.nextGaussian() generates a double value with a probability density of a Gaussian (i.e., normal ) distribution (mean of 0.0 and standard deviation of 1.0). 20.8 Bit Manipulation and the Bitwise Operators Java provides extensive bit-manipulation capabilities for programmers who need to get down to the so-called bits-and-bytes level. Operating systems, test equipment software, networking software and many other kinds of software require that the programmer communicate directly with the hardware. In this section and the next, we discuss Java s bit- manipulation capabilities. We introduce Java s bitwise operators, and we demonstrate their use in live-code examples. Computers represent all data internally as sequences of bits. Each bit can assume the value 0 or the value 1. On most systems, a sequence of 8 bits forms a byte the standard storage unit for a variable of type byte. Other data types are stored in larger numbers of bytes. The bitwise operators can manipulate the bits of integral operands (i.e., those having type byte, char, short, int and long). Note that the bitwise operator discussions in this section show the binary representations of the integer operands. For a detailed explanation of the binary (also called base 2) number system, see Appendix E, Number Systems. The bitwise operators are bitwise AND (&), bitwise inclusive OR (|), bitwise exclusive OR (^), left shift (<<), right shift with sign extension (>>), right shift with zero extension (>>>) and complement (~). The bitwise AND, bitwise inclusive OR and bitwise exclusive OR operators compare their two operands bit by bit. The bitwise AND operator sets each bit in the result to 1 if the corresponding bit in both operands is 1. The bitwise inclusive OR operator sets each bit in the result to 1 if the corresponding bit in either (or both) operand(s) is 1. The bitwise exclusive OR operator sets each bit in the result to 1 if the corresponding bit in exactly one operand is 1. The left shift operator shifts the bits of its left operand to the left by the number of bits specified in its right operand. The right shift operator with sign extension shifts the bits in its left operand to the right by the number of bits specified in its right operand if the left operand is negative, 1s are shifted in from the left; otherwise, 0s are shifted in from the left. The right shift operator with zero extension shifts the bits in its left operand to the right by the number of bits specified in its right operand 0s are shifted in from the left. The bitwise complement operator sets all 0 bits in its operand to 1 in the result and sets all 1 bits to 0 in the result. Detailed discussions of each bitwise operator appear in the following examples. The bitwise operators are summarized in Fig. 20.5.
Go visit our java server pages services for a reliable, lowcost webhost to satisfy all your needs.