Q:

How to set a particular bit in C?

0

How to set a particular bit in C?

All Answers

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

Answer:

Setting a Bits

Bitwise OR operator (|) use to set a bit of integral data type.”OR” of two bits is always one if any one of them is one.

An algorithm to set the bits

Number  | =  (1<< nth Position)

A simple program to set a bit:

#include <stdio.h>
int main(int argc, char *argv[])
{
    unsigned char cData=0x00;
    int iPos =0;
    printf("cData = 0x%x\n\n",cData);
    printf("Enter the position which you want set = ");
    scanf("%d",&iPos);
    //Set the nth bit.
    cData|=1<<iPos;
    //Print the data
    printf("\n\n%dth Bit Set 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 clear a particular bit in C?... >>
<< Write a program to check an integer is a power of ...