1. Set or Clear a Specific Bit in a Register

Back To All Submissions
Previous Submission
Next Submission

Code

#include <stdio.h>

int main() {
    int reg;   // thanh ghi 8 bit (0-255)
    int pos;   // vị trí bit (0-7)
    int mode;  // 1: set bit, 0: clear bit

    scanf("%d %d %d", &reg, &pos, &mode);

    if (mode == 1) {
        // SET bit
        reg = reg | (1 << pos);
    } else {
        // CLEAR bit
        reg = reg & ~(1 << pos);
    }

    printf("%d", reg);

    return 0;
}

Solving Approach

 

 

 

Was this helpful?
Upvote
Downvote