All submissions

Set or Clear a Specific Bit in a Register

Solving Approach

Step 1: First you need to check the mode if the mode is 1 set the bit else clear the bit.

Step 2: set means r|(1<<p). Clear means r& ~(1<<p)

 

 

 

Code

#include <stdio.h>

unsigned char modifyBit(unsigned char r, int p, int m) {
    if (m==1){
        return r|(1<<p);
    }
    else{
        return r& ~(1<<p);
    }
    
    }

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

 

Loading...

Input

10 3 1

Expected Output

10