|
|
|
|
|
|
|
|
|
|
|
|
|
Bitwise operators |
|
|
|
|
Logical operators |
|
|
|
|
&&, || |
and, or |
|
|
|
|
operate on entire value |
|
|
|
|
int x = 0, y = 1; |
|
|
|
|
(x && y) |
value 0 |
|
|
|
|
(x || y) |
value 1 |
|
|
|
May want to work with
individual bits |
|
|
|
|
int x = 2, y = 7; |
|
|
|
|
(x
&& y) |
value 1 |
|
|
|
|
What about bits? |
|
|
|
value |
|
x |
0000 |
0000 |
0000 |
0000 |
0000 |
0000 |
0000 |
0010 |
2 |
|
y |
0000 |
0000 |
0000 |
0000 |
0000 |
0000 |
0000 |
0111 |
7 |
|
|
|
|
|
|
|
|
Bitwise and |
|
|
|
|
|
|
x & y |
0000 |
0000 |
0000 |
0000 |
0000 |
0000 |
0000 |
0010 |
2 |
|
|
|
|
|
|
|
|
|
|
Bitwise or |
|
|
|
|
|
|
|
x | y |
0000 |
0000 |
0000 |
0000 |
0000 |
0000 |
0000 |
0111 |
7 |
|
|
|
|
|
|
|
|
Bitwise xor
(exclusive-or) |
|
|
|
|
|
x ^ y |
0000 |
0000 |
0000 |
0000 |
0000 |
0000 |
0000 |
0101 |
5 |
|
Value of a bit is 1 if
only one bit is 1 |
|
|
|
|
|
|
|
|
|
|
Complement |
|
|
|
|
|
|
~x |
1111 |
1111 |
1111 |
1111 |
1111 |
1111 |
1111 |
1101 |
|
|
~y |
1111 |
1111 |
1111 |
1111 |
1111 |
1111 |
1111 |
1000 |
|
|
Each bit is
"flipped" to opposite value |
|
|
|
How is this related to
negative value? |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|