All submissions

Set or Clear a Specific Bit in a Register

Code

#include <stdio.h>

int main() {
    unsigned int regValue; // Register value (0-255)
    int bitPos, mode;

    // Input: register value, bit position, and mode
    scanf("%u %d %d", &regValue, &bitPos, &mode);

    // Ensure values are within valid range
    if (regValue > 255 || bitPos < 0 || bitPos > 7 || (mode != 0 && mode != 1)) {
        printf("Invalid input\n");
        return 1;
    }

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

    // Print updated register value
    printf("%u\n", regValue);

    return 0;
}

Solving Approach

 

 

 

Loading...

Input

10 3 1

Expected Output

10