1. Set or Clear a Specific Bit in a Register

Back To All Submissions
Previous Submission
Next Submission

Code

#include <stdio.h>

int main() {
    unsigned char reg;   // 8-bit register value (0–255)
    int pos;             // bit position (0–7)
    int mode;            // operation: 1=set, 0=clear

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

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

    // Output
    printf("%u\n", reg);

    return 0;
}

Solving Approach

 

 

 

Was this helpful?
Upvote
Downvote