Q:

How to clear a particular bit in C?

0

How to clear a particular bit in C?

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Bitwise AND operator (&) use to clear a bit of integral data type. “AND” of two bits is always zero if any one of them is zero.

An algorithm to clear the bits

Number  &=  ~ (1<< nth Position)
To clear the nth bit, first, you need to invert the string of bits then AND it with the number.

A simple program to clear a bit:

#include <stdio.h>
int main(int argc, char *argv[])
{
    unsigned char cData=0xFF;
    int iPos =0;
    printf("Initially cData = 0x%x\n\n",cData);
    printf("Enter the position which you want clear = ");
    scanf("%d",&iPos);
    //clear the nth bit.
    cData &= ~(1<<iPos);
    //Print the data
    printf("\n\n%dth Bit clear Now cData will be = 0x%x\n",iPos,cData);
    return 0;
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

How to check if a particular bit is set in C?... >>
<< How to set a particular bit in C?...