1. Set or Clear a Specific Bit in a Register

Discussions4
Log in to post comments and replies.
You
Loading editor...
rohithsantosh
rohithsantosh
May 17 2026

#include <stdio.h>
#define SET_BIT(reg, pos)   ((reg) |=  (1U << (pos)))
#define CLR_BIT(reg, pos)   ((reg) &= ~(1U << (pos)))

unsigned char modifyBit(unsigned char reg, int pos, int mode) {
   
   if(mode == 1){
       SET_BIT(reg,pos);
   }
   else{
       CLR_BIT(reg,pos);
   }
   return reg;
}

int main() {
   unsigned char reg;
   int pos, mode;
   scanf("%hhu %d %d", &reg, &pos, &mode);
   printf("%d", modifyBit(reg, pos, mode));
   return 0;
}

0
GauthamShankar
GauthamShankar
Mar 04 2026
    if(mode==1){
        reg |= (1U<<pos);
    }
    else reg&=~(1U<<pos);
    return reg;
0
MohanKilari
MohanKilari
Jun 15 2026
#include <stdio.h>

unsigned char modifyBit(unsigned char reg, int pos, int mode) {
    // Write your code here
    if(mode == 1){
        reg |= (1<< pos);
    }
    else if(mode==0){
        reg&=~(1<<pos);
    }
    return reg;
}

int main() {
    unsigned char reg;
    int pos, mode;
    scanf("%hhu %d %d", &reg, &pos, &mode);
    printf("%d", modifyBit(reg, pos, mode));
    return 0;
}
+1
VELMURUGANPMCT
VELMURUGANPMCT
Jun 15 2026

i dont understand 

 

0