The bitwise logical operators perform bit by bit logical operations on variables. Here are the 6 bitwise operators of C :
Operator | Description | Type |
---|---|---|
& |
AND | binary |
| |
OR | binary |
^ |
XOR | binary |
~ |
NOT | unary |
>> |
Right shift | binary |
<< |
Left shift | binary |
A logical AND is a binary operation whose output s
is true only if
both inputs (a
and b
) are true:
a | b | s |
---|---|---|
0 | 0 | 0 |
0 | 1 | 0 |
1 | 0 | 0 |
1 | 1 | 1 |
When this operator is used on variables, it is applied bit by bit. That is to say that the first bit of the result is the logical AND of the first bit of the first operand and of the first bit of the second operand. We will come back to this in detail in the next lesson.
An OR is a binary operation whose output s
is true if
a
or b
);a
and b
):a | b | s |
---|---|---|
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 1 |
An exclusive OR (XOR) is a binary operation whose output s
is true if and only
if one of the two inputs (a
and b
) is true:
a | b | s |
---|---|---|
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 0 |
When speaking, we do not distinguish between OR and exclusive OR. Here are two examples:
NOT is a unary operation whose output s
is true if and only if the input a
is false. This
operator inverts the input bits:
a | s |
---|---|
0 | 1 |
1 | 0 |
In C, what is the bitwise AND operator?
&
is the bitwise AND operator.
Try again...
In C, what is the bitwise OR operator?
|
is the bitwise OR operator.
Try again...
In C, what is the bitwise NOT operator?
~
is the bitwise NOT operator.
Try again...
In C, what is the purpose of the ^
operator?
Which are the binary operators (which accept two operands)?
~
) necessarily a unary operator.
Try again...