1. Set or Clear a Specific Bit in a Register

Back To All Submissions
Previous Submission
Next Submission

Code

#include <stdio.h>
#define setbit(reg, pos) (reg |= (1<<pos))
#define clearbit(reg, pos) (reg &= ~(1<<pos))

unsigned char modifyBit(unsigned char reg, int pos, int mode) {
    // Write your code here
    if (mode == 0x1) {
        setbit(reg, pos);
    } else if (mode ==  0x0) {
        clearbit(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;
}

Solving Approach

 

 

 

Was this helpful?
Upvote
Downvote