1. Set or Clear a Specific Bit in a Register

Discussions5
Log in to post comments and replies.
You
Loading editor...
23ECE41RenukadeviK

#include<stdio.h>

int main()
{
   int reg, pos, mode;

   printf("Enter register value: ");
   scanf("%d", &reg);

   printf("Enter bit position (0-7): ");
   scanf("%d", &pos);

   printf("Enter mode (1=set, 0=clear): ");
   scanf("%d", &mode);

   if(mode == 1)
   {
       reg = reg | (1 << pos);  
       printf("After Set = %d\n", reg);
   }
   else
   {
       reg = reg & ~(1 << pos);  
       printf("After Clear = %d\n", reg);
   }

   return 0;
}

0
rohithsantosh
rohithsantosh
Jul 30 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;
}

+2
GauthamShankar
GauthamShankar
Mar 04 2026
    if(mode==1){
        reg |= (1U<<pos);
    }
    else reg&=~(1U<<pos);
    return reg;
0
MohanKilari
MohanKilari
Jul 30 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;
}
+2
VELMURUGANPMCT
VELMURUGANPMCT
Jun 15 2026

i dont understand 

 

0