HTML and CSS Reference
In-Depth Information
Table 5.15 Bitwise Operators
Operator
Function
Example
What It Does
&
Bitwise AND
x & y
Returns a 1 in each bit position if both
corresponding bits are 1 .
Bitwise OR
x | y
Returns a 1 in each bit position if one
or both corresponding bits are 1 .
^
Bitwise XOR
x ^ y
Returns a 1 in each bit position if one,
but not both, of the corresponding bits
are 1 .
~
Bitwise NOT
~x
Inverts the bits of its operands.
1 becomes 0 ; 0 becomes 1 .
<<
Left shift
x << y
Shifts x in binary representation y bits
to left, shifting in zeros from the right.
>>
Right shift
x >> y
Shifts x in binary representation y bits
to right, discarding bits shifted off.
>>>
Zero-fill right shift
x >>> b
Shifts x in binary representation y bits
to the right, discarding bits shifted off,
and shifting in zeros from the left.
When using the bitwise operations & , | , ^ , and ~ , each bit in the first operand is paired
with the corresponding bit in the second operand: first bit to first bit, second bit to sec-
ond bit, and so on. For example, the binary representation fo r 5 & 4 is 101 & 100.
101
101
101
& 100
| 100
^ 100
-----
-----
-----
100
101
001
Bitwise Shift Operators. The bitwise shift operators take two operands: The first is
a quantity to be shifted, and the second specifies the number of bit positions by which
the first operand is to be shifted. The direction of the shift operation is controlled by the
operator used.
<< (left shift)
This operator shifts the first operand the specified number of bits to the left.
Excess bits shifted off to the left are discarded. Zero bits are shifted in from the
right.

Search WWH ::

Custom Search