# Lesson 7.6. Overview of bit masking

The table below summarizes the three types of masking:

Description Operator C operator `0` in the mask `1` in the mask
Forçage à zéro bitwise AND `&` Clear the bit Keep the bit
Forçage à un bitwise OR `|` Keep the bit Set the bit
Inversion de bits bitwise XOR `^` Keep bit Toogle the bit

## Exercise 1

\$\$ reg = ( b_7 \: b_6 \: b_5 \: b_4 \; b_3 \: b_2 \: b_1 \: b_0 )_2 \$\$

Complete the program below to:

• set bits \(b_0\) and \(b_1\)
• clear bits \(b_2\) and \(b_3\)
• toggle bits \(b_4\) and \(b_5\)
• keep bits \(b_6\) and \(b_7\)
``````// | 0 | 1 | 0 | 1 | 0 | 1 | 0 | 1 |
unsigned char reg = 0x55;
printf ("Before  : ");
affBin(reg);

// Set b0 and b1
// COMPLETE HERE

// Clear b2 and b3
// COMPLETE HERE

// Toggle b4 and b5
// COMPLETE HERE

// Display the result
printf ("After   : ");
affBin(reg);  ``````

## Exercise 2

Complete the program below to:

• clear the 4 least significant bits ;
• set the 4 most significant bits.
``````// | 0 | 1 | 0 | 1 | 0 | 1 | 0 | 1 |
unsigned char reg = 0x55;
printf ("Before  : ");
affBin(reg);

// Clear the 4 least significant bits and set the 4 least significant bits.
// COMPLETE HERE

// Display the result
printf ("After   : ");
affBin(reg);  ``````

## Quiz

What operator should be used to invert some bits while keeping the others?

What operator should be used to clear a bit?

What operator should be used to set a bit?

Which instructions clears the 8 bits?