All submissions

Set or Clear a Specific Bit in a Register

Code

#include <stdio.h>

int main() {
    unsigned char reg_val;  // 8-bit register value
    int bit_pos, operation;

    // Input: register value, bit position (0-7), operation (0-clear, 1-set)
    scanf("%hhu %d %d", &reg_val, &bit_pos, &operation);

    if (bit_pos < 0 || bit_pos > 7 || (operation != 0 && operation != 1)) {
        printf("Invalid input\n");
        return 1;
    }

    if (operation == 1) {
        // Set the bit
        reg_val |= (1 << bit_pos);
    } else {
        // Clear the bit
        reg_val &= ~(1 << bit_pos);
    }

    // Output the modified register value
    printf("%d\n", reg_val);

    return 0;
}

Solving Approach

 

 

 

Loading...

Input

10 3 1

Expected Output

10