...

/

The Utility of the OR Bitwise Operator

The Utility of the OR Bitwise Operator

Get introduced to the utility of the OR bitwise operator.

We'll cover the following...

Switch a bit on using OR

The OR bitwise operator is used to turn on the bit. See the code given below!

Press + to interact
#include <stdio.h>
# define BV(x) ( 1 << x)
int main( )
{
// Declare variables
unsigned char n ;
unsigned int val ;
n = 150;
// Check whether the 3rd bit is on or off
if ( ( n & BV(3) ) == BV(3) )
printf( "3rd bit is on\n" ) ;
else
{
printf ( "3rd bit is off\n" ) ;
// Put on 3rd bit
n = n | BV(3) ;
printf ( "%d\n", n ) ;
}
}

To turn on the bit, first, we have to select the appropriate mask value based on the ...

Access this course and 1400+ top-rated courses and projects.