Search⌘ K
AI Features

The Utility of the OR Bitwise Operator

Explore how to manipulate bits in C by using the OR bitwise operator to switch on specific bits. Understand how to create masks and apply the operator without affecting other bits, improving your control over bit-level data manipulation.

We'll cover the following...

Switch a bit on using OR

The OR ...

C
#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 ...