All submissions

Set the Bit in an 8-bit Register

Code

#include <stdio.h>
#include <stdint.h>

// Function to set a bit at a given position
uint8_t setBit(uint8_t reg, int pos) {
    if (pos < 0 || pos > 7) {
        return reg; // ignore invalid bit positions
    }
    return reg | (1 << pos);
}

int main() {
    uint8_t reg;
    int pos;

    //printf("Enter register value (0-255): ");
    scanf("%hhu", &reg);   // %hhu for unsigned 8-bit

    //printf("Enter bit position (0-7): ");
    scanf("%d", &pos);

    printf("%d\n", setBit(reg, pos));

    return 0;
}

Solving Approach
 

 

 

Loading...

Input

5 1

Expected Output

7