87
This expression...
... resolves to this value.
!(“Hello”==“Hello”)
0
Bitwise-And (&) Operator
The bitwise-and operator performs the logical AND operation bit by bit between two integers. This is
better understood by looking at the example of
12&10
. The binary value of
12
is
1100
, and the binary
value of
10
is
1010
.
12&10
is computed by performing an AND between each of the corresponding
four bits:
12:
1
1
0
0
10:
1
0
1
0
12&10:
1
0
0
0
Therefore:
12&10 = (Binary) 1000 = (Decimal) 8
Bitwise-Or (|) Operator
The bitwise-or operator performs the logical OR operation bit by bit between two integers. This is better
understood by looking at the example of
12|10
. The binary value of
12
is
1100
, and the binary value
of
10
is
1010
.
12I10
is computed by performing an OR between each of the corresponding four bits:
12:
1
1
0
0
10:
1
0
1
0
12|10:
1
1
1
0
Therefore:
12|10 = (Binary) 1110 = (Decimal) 14
One's Complement (~) Operator
The one's complement operator is a bitwise-not prefix operator that performs a logical NOT to each bit of
an integer. This is better understood by looking at the example of
~5
. The binary value of
5
is
101
.
~5
is computed by performing a NOT on each of the three bits:
5:
1
0
1
~5:
0
1
0
Therefore: ~
5 = (Binary) 010 = (Decimal) 2
Realize that the bitwise not is performed on a 32-bit value, e.g.
~0
converts to a 32-bit value with all bits
in the integer set to 1. To get just the bits you want, you should AND the result with the bits of interest,
e.g.
~5 & 7
.