Java Reference
In-Depth Information
appendix
C
bitwise operators
J ava provides bitwise operators for the bit-by-bit manipulation of integers.
This process allows the packing of several Boolean objects into an integral type.
The operators ~ (unary complement), << and >> (left and right shift), & (bitwise
AND), ^ (bitwise exclusive OR), | (bitwise OR), and assignment operators cor-
responding to all these operators except unary complement. Figure C.1 illus-
trates the result of applying these operators. Note that >> is considered a
signed bit shift: the value that is filled in the highest bit can depend on sign
extension. >>> is considered an unsigned bit shift and is used to guarantee that
the highest bit is filled with a 0.
The precedence and associativity of the bitwise operators are somewhat
arbitrary. When working with them, you should use parentheses.
Java provides bit-
wise operators for
the bit-by-bit
manipulation
of integers. This
process allows the
packing of several
Boolean objects
into an integral type.
figure C.1
Examples of bitwise
operators
//Pretend ints are 16 bits
int a = 3737; // 0000111010011001
int b = a << 1; // 0001110100110010
int c = a >> 2; // 0000001110100110
int d = 1 << 15; // 1000000000000000
int e = a | b; // 0001111110111011
int f = a & b; // 0000110000010000
int g = a ^ b; // 0001001110101011
int h = ~g; // 1110110001010100
 
Search WWH ::




Custom Search