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 |
$$ reg = ( b_7 \: b_6 \: b_5 \: b_4 \; b_3 \: b_2 \: b_1 \: b_0 )_2 $$
Complete the program below to:
// | 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);
Complete the program below to:
// | 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);
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?
reg |= 0x00;
reg &= 0x00;
reg &= 0xFF;