1. Bitwise &
operator
We previously said that all data is stored in memory in a binary representation. So quite a long time ago, programmers came up with a lot of interesting ways to work with binary numbers. For example, Java has logical operators that operate on the bits of a number's binary representation: &
(AND), | (OR)
, ~
(NOT or complement) and ^
(XOR - exclusive or).
a & b
This operator is very similar to the logical &
(AND) operator, only it is denoted by a single ampersand, not two:
And it is applied to individual bits. Each operand is treated as an array of bits, and the i
th bit of the result is calculated using the i
th bit of each of the two operands.
The first bit of the result will be calculated based on the first bit of the number a
and the first bit of the number b
, the second bit — based on the second bit of the number a
and the second bit of the number b
, etc.
The &
(AND) operator means "the resulting bit is equal to one only if the corresponding bit of the number a
is equal to one AND
the corresponding bit of the number b
is equal to one":
1 & 1 = 1
1 & 0 = 0
0 & 1 = 0
0 & 0 = 0
Examples:
Example | Result |
---|---|
|
|
|
|
|
|
|
|
2. Bitwise |
operator
This operator is very similar to the logical |
(OR) operator, only it is denoted by a single vertical line, not two:
a | b
And it is applied to individual bits. Each operand is treated as an array of bits, and the ith bit of the result is calculated using the ith bit of each of the two operands.
The bitwise |
(OR) operator means "the resulting bit is equal to one if the corresponding bit of the number a
is equal to one OR
the corresponding bit of the number b
is equal to one":
1 | 1 = 1
1 | 0 = 1
0 | 1 = 1
0 | 0 = 0
Examples:
Example | Result |
---|---|
|
|
|
|
|
|
|
|
Only when the corresponding bits (the bits at the same position) of both numbers are zero is the result's corresponding bit equal to zero.
3. Bitwise ^
(XOR or "exclusive or") operator
The XOR
operator, also pronounced exclusive or, is denoted by the ^
symbol. To enter it on the keyboard, press shift + 6 (on a English keyboard layout).
a ^ b
This operator is somewhat similar to the OR
operator, including in that it has a similar name: XOR
The bitwise ^
(XOR) operator means "the resulting bit is equal to one if the corresponding bit of the number a
is equal to one OR
the corresponding bit of the number b
is equal to one but not both at the same time":
1 ^ 1 = 0
1 ^ 0 = 1
0 ^ 1 = 1
0 ^ 0 = 0
Examples:
Example | Result |
---|---|
|
|
|
|
|
|
|
|
Only when the corresponding bits (the bits at the same position) of both numbers are different is the result's corresponding bit equal to one. If the bits are the same, the resulting bit is equal to zero.
4. Bitwise ~
(NOT, COMPLEMENT) operator
I think you can already guess what it does. This operator is very similar to the logical !
(NOT) operator, but it is denoted by a tilde, not an exclamation point:
~a
This is a unary operator, which means it applies to a single number, not two. It appears before this single operand.
The bitwise ~
operator means "the resulting bit is one if the corresponding bit of the number a
is zero, and it is zero if the corresponding bit of the number a
is one":
~1 = 0
~0 = 1
Examples:
Example | Result |
---|---|
|
|
|
|
|
|
|
|
This operator simply changes the bit that are 1
to 0
and bits that are 0
to 1
.
GO TO FULL VERSION