Search⌘ K
AI Features

The Utility of the AND Bitwise Operator

Explore the utility of the AND bitwise operator in C to check if specific bits are on or off and to switch off particular bits without affecting others. Understand how to create appropriate mask values and apply bitwise operations for effective bit manipulation.

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!

C
#include <stdio.h>
# define BV(x) ( 1 << x)
int main( )
{
// Declare variables
unsigned char n ;
unsigned int val ;
n = 120;
// Check 3rd bit is on or off
if ( ( n & BV(3) ) == BV(3) )
{
// Display 3rd bit is on
printf( "3rd bit is on\n" ) ;
}
// Display 3rd bit is off
else
printf ( "3rd bit is off\n" ) ;
}

To check whether the 3rd3^{rd} ...