Java Reference
In-Depth Information
table 2-10 (continued)
bit Wise oper ator
meaning
ex amples
result
Bitwise exclusive OR (XOR) operator: Puts
a 1 bit in the result if one of the operands,
but not both, has a 1 bit at the given
position.
a: 0010 1000
b: 0111 1010
 
r: 0101 0010
^
a^b;
Unary bitwise inverse operator: Changes
every 1 bit to 0 and every 0 bit to 1.
a: 0010 1000
r: 1101 0111
~
~a
a>>2  
Signed right shift operator: Shifts the left
operand to the right by the number of bits
specified. The left digits of a positive number
are then filled with 0s, while the left digits
of a negative number are filled with 1s. This
preserves the original sign of the number,
hence the name “signed right shift.”
a: 0010 1000
r: 0000 1010
 
>>
c>>2
c: 1111 0100
r: 1111 1101
Unsigned right shift operator: Shifts the
left operand to the right by the specified
number of bits. The left digits are always
filled with 0s, regardless of the sign, hence
the name “unsigned right shift.”
a>>>3  
a: 0010 1000
r: 0000 0101
 
 
c: 1111 0100
r: 0001 1110
>>>
c>>>3
Left shift operator: Shifts the left operand
to the left by the number of bits indicated.
The right digits are then filled with 0s.
Since only the right side is filled, it is not
possible to fill with 1s or 0s to ensure a
positive or negative number. Therefore
there is no distinction between a “signed
left shift” and an “unsigned left shift.”
a<<2  
a: 0010 1000
r: 1010 0000
 
 
c: 1111 0100
r: 1101 0000
<<
c<<2
Logical Operators
A logical operator returns a Boolean result based on the Boolean result of one or more expressions.
For this reason, they may also be called Boolean operators. Logical or Boolean operators are always
evaluated from left to right. Consider, for example, the following expressions and their Boolean
results. Table 2-11 then illustrates the evaluation of the logical operators that can be used in Java on
these expressions.
A: 3 > 2 (True)
B: 2 < 1 (False)
 
Search WWH ::




Custom Search