Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
A shift operator performs bit manipulation on data by shifting the bits of its first operand right or left. This table summarizes the shift operators available in the Java programming language.
Operator Use Operation >>
op1 >> op2
shift bits of op1
right by distanceop2
<<
op1 << op2
shift bits of op1
left by distanceop2
>>>
op1 >>> op2
shift bits of op1
right by distanceop2
(unsigned)Each operator shifts the bits of the left-hand operand over by the number of positions indicated by the right-hand operand. The shift occurs in the direction indicated by the operator itself. For example, the following statement shifts the bits of the integer 13 to the right by one position:
The binary representation of the number 13 is 1101. The result of the shift operation is 1101 shifted to the right by one position-110, or 6 in decimal. The left-hand bits are filled with 0s as needed.13 >> 1;The following table shows the four operators the Java programming language provides to perform bitwise functions on their operands:
Operator Use Operation &
op1 & op2
bitwise and
|
op1 | op2
bitwise or
^
op1 ^ op2
bitwise xor
~
~op2
bitwise complement When its operands are numbers, the
&
operation performs the bitwiseAND
function on each parallel pair of bits in each operand. TheAND
function sets the resulting bit to 1 if the corresponding bit in both operands is 1, as shown in the following table.
op1
op2
Result 0 0 0 0 1 0 1 0 0 1 1 1 Suppose that you were to
AND
the values 13 and 12, like this:13 & 12
. The result of this operation is 12 because the binary representation of 12 is 1100, and the binary representation of 13 is 1101.If both operand bits are 1, the1101 //13 & 1100 //12 ------ 1100 //12AND
function sets the resulting bit to 1; otherwise, the resulting bit is 0. So, when you line up the two operands and perform theAND
function, you can see that the two high-order bits (the two bits farthest to the left of each number) of each operand are 1. Thus, the resulting bit in the result is also 1. The low-order bits evaluate to 0 because either one or both bits in the operands are 0. When both of its operands are numbers, the|
operator performs the inclusive or operation, and^
performs the exclusive or (XOR
) operation. Inclusive or means that if either of the two bits is 1, the result is 1. The following table shows the results of inclusive or operations:
op1
op2
Result 0 0 0 0 1 1 1 0 1 1 1 1 Exclusive or means that if the two operand bits are different the result is 1, otherwise the result is 0. The following table shows the results of an exclusive or operation.
op1
op2
Result 0 0 0 0 1 1 1 0 1 1 1 0 And finally, the complement operator inverts the value of each bit of the operand: if the operand bit is 1 the result is 0 and if the operand bit is 0 the result is 1.
Among other things, bitwise manipulations are useful for managing sets of boolean flags. Suppose, for example, that your program had several boolean flags that indicated the state of various components in your program: is it visible, is it draggable, and so on. Rather than define a separate boolean variable to hold each flag, you could define a single variable,
flags
, for all of them. Each bit withinflags
would represent the current state of one of the flags. You would then use bit manipulations to set and to get each flag. First, set up constants that indicate the various flags for your program. These flags should each be a different power of 2 to ensure that each bit is used by only one flag. Define a variable,flags
, whose bits would be set according to the current state of each flag. The following code sample initializesflags
to 0, which means that all flags arefalse
(none of the bits are set).To set the "visible" flag when something became visible you would use this statement:static final int VISIBLE = 1; static final int DRAGGABLE = 2; static final int SELECTABLE = 4; static final int EDITABLE = 8; int flags = 0;To test for visibility, you could then write:flags = flags | VISIBLE;Here's the complete program,if ((flags & VISIBLE) == VISIBLE) { ... }BitwiseDemo
, that includes this code.Here's the output from this program:public class BitwiseDemo { static final int VISIBLE = 1; static final int DRAGGABLE = 2; static final int SELECTABLE = 4; static final int EDITABLE = 8; public static void main(String[] args) { int flags = 0; flags = flags | VISIBLE; flags = flags | DRAGGABLE; if ((flags & VISIBLE) == VISIBLE) { if ((flags & DRAGGABLE) == DRAGGABLE) { System.out.println("Flags are Visible and Draggable."); } } flags = flags | EDITABLE; if ((flags & EDITABLE) == EDITABLE) { System.out.println("Flags are now also Editable."); } } }Flags are Visible and Draggable. Flags are now also Editable.
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2004 Sun Microsystems, Inc. All rights reserved.