1. Set or Clear a Specific Bit in a Register

Back To All Submissions
Previous Submission
Next Submission

Code

#include <stdio.h>

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

int main() {
    unsigned int reg; // 1 Byte 8bit
    int pos, mode; // 4 byte hoặc 8 byte
    scanf("%hhu %d %d", &reg, &pos, &mode);
    printf("%d", modifyBit(reg, pos, mode));
    return 0;
}

Solving Approach

 

 

 

Was this helpful?
Upvote
Downvote