Set, Clear and Read Register Bits using Bit Positions

In order to set a bit from a register using bit positions, the binary OR operator will be applied between the register and the value resulting from shifting ‘1’ with the value of the bit position. To clear a bit using bit positions the binary AND operator is used with the negated value of the shifting result.

ADCON0 |= (1 << _ADCON0_ADON_POSITION);  /* the ADC Enable bit is set */
ADCON0 &= ~(1 << _ADCON0_ADON_POSITION); /* the ADC Enable bit is cleared */
Note: The bit position for the ADC Enable bit (ADON) has the following declaration in the header file.
#define _ADCON0_ADON_POSITION                               0x7

The code listing below shows how to read the value of a PORT pin using bit positions and execute a set of instructions if that pin is low.

if(PORTA & (1<< _PORTA_RA0_POSITION))
{
    /* set of instructions */
}