The Utility of the AND Bitwise Operator
Get introduced to the utility of & operator.
We'll cover the following...
The AND &
operator is used to:
- Check whether a bit is on or off
- Switch off a particular bit
Check whether a bit is on or off
See the code given below!
Press + to interact
#include <stdio.h># define BV(x) ( 1 << x)int main( ){// Declare variablesunsigned char n ;unsigned int val ;n = 120;// Check 3rd bit is on or offif ( ( n & BV(3) ) == BV(3) ){// Display 3rd bit is onprintf( "3rd bit is on\n" ) ;}// Display 3rd bit is offelseprintf ( "3rd bit is off\n" ) ;}
To check whether the bit is on or off, we need to, first, select the appropriate mask ...
Access this course and 1400+ top-rated courses and projects.