Set or Clear a Specific Bit in a Register

Code

#include <stdio.h>

int main() {
    unsigned char reg;
    int bitPos, mode;

    // Read input directly
    scanf("%hhu %d %d", &reg, &bitPos, &mode);

    if (mode == 1) {
        reg = reg | (1 << bitPos);      // Set bit
    } else {
        reg = reg & ~(1 << bitPos);     // Clear bit
    }

    // Print only the result
    printf("%u", reg);

    return 0;
}

Solving Approach

 

 

 

Upvote
Downvote
Loading...

Input

10 3 1

Expected Output

10