1176 Java Utilities (Web hosting companies) Package and Bit Manipulation Chapter

1176 Java Utilities Package and Bit Manipulation Chapter 20 Operator Name Description & bitwise AND The bits in the result are set to 1if the corresponding bits in the two operands are both 1. | bitwise inclusive OR The bits in the result are set to 1if at least one of the corresponding bits in the two operands is 1. ^ bitwise exclusive OR The bits in the result are set to 1if exactly one of the corresponding bits in the two operands is 1. << left shift Shifts the bits of the first operand left by the number of bits specified by the second operand; fill from the right with 0 bits. >> right shift with sign extension Shifts the bits of the first operand right by the number of bits specified by the second operand. If the first operand is negative, 1s are shifted in from the left; otherwise, 0s are shifted in from the left. >>> right shift with zero extension Shifts the bits of the first operand right by the number of bits specified by the second operand; 0s are shifted in from the left. ~ one s complement All 0bits are set to 1and all 1bits are set to 0. Fig. 20.5The bitwise operators . g. 20.5 When using the bitwise operators, it is useful to display values in their binary representation to illustrate the effects of these operators. The application of Fig. 20.6 allows the user to enter an integer into a JTextField and press Enter. Method actionPerformed(lines 32 36) reads the Stringfrom the JTextField, converts it to an integer and invokes method getBits (lines 55 80) to obtain a String representation of the integer in bits. The result is displayed in the output JTextField. The integer is displayed in its binary representation in groups of eight bits each. Method getBitsuses the bitwise AND operator to combine variable value with variable displayMask. Often, the bit- wise AND operator is used with an operand called a mask an integer value with specific bits set to 1. Masks are used to hide some bits in a value while selecting other bits. In get- Bits, mask variable displayMask is assigned the value 1<<31or 10000000 00000000 00000000 00000000 The left shift operator shifts the value 1from the low-order (rightmost) bit to the high-order (leftmost) bit in displayMaskand fills in 0bits from the right. 1 // Fig. 20.6: PrintBits.java 2 // Printing an unsigned integer in bits 3 4 // Java core packages 5 import java.awt.*; 6 import java.awt.event.*; Fig. 20.6Printing the bits in an integer (part 1 of 3). Fig. 20.6
Note: In case you are looking for affordable webhost to host and run your servlet application check Vision make web site services

Leave a Reply