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

AND logical gate

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

Inclusive OR logical gate

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

a b s
0 0 0
0 1 1
1 0 1
1 1 1

Bitwise exclusive OR (XOR)

Exclusive OR logical gate (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:

Bitwise NOT

NOT logical gate

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?

Check Bravo ! & is the bitwise AND operator. Try again...

In C, what is the bitwise OR operator?

Check Bravo ! | is the bitwise OR operator. Try again...

In C, what is the bitwise NOT operator?

Check Bravo ! ~ is the bitwise NOT operator. Try again...

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

Check Good for you! In C, the circumflex is the exclusive OR operator and not the exponent. Try again...

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

Check Bravo ! NOT (~) necessarily a unary operator. Try again...

See also


Last update : 11/03/2022