# Lesson 3.5. Bitwise operators

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

## Bitwise AND

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.

## Bitwise inclusive OR

An OR is a binary operation whose output s is true if

• one of the inputs is true (a or b);
• both inputs are true (a and b):
a b s
0 0 0
0 1 1
1 0 1
1 1 1

## Bitwise exclusive OR (XOR)

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:

• France or Italy will win the World Cup This is an exclusive OR because the two cannot win in the same year: it is one or the other.
• You can do either exercise 1 or exercise 2. This is an inclusive OR because it is possible to do exercise 1, exercise 2 or both. Sometimes the notation and/or is used, which corresponds to the inclusive OR.

## Bitwise NOT

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

## Quiz

In C, what is the bitwise AND operator?

In C, what is the bitwise OR operator?

In C, what is the bitwise NOT operator?

In C, what is the purpose of the ^ operator?

Which are the binary operators (which accept two operands)?