Set the Bit in an 8-bit Register

Code

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

// Function to set a specific bit in an 8-bit register
uint8_t set_bit(uint8_t reg, uint8_t pos) {
    reg |= (1 << pos);   // Set the bit at position 'pos'
    return reg;          // Return updated register
}

int main() {
    uint8_t reg, pos;

    // Input: register value and bit position
    scanf("%hhu %hhu", &reg, &pos);

    // Compute the updated register
    uint8_t result = set_bit(reg, pos);

    // Output the modified register
    printf("%u", result);

    return 0;
}

Solving Approach
 

 

 

Upvote
Downvote
Loading...

Input

5 1

Expected Output

7