Set the Bit in an 8-bit Register

Code

#include <stdio.h>

// Function to set a bit at a given position
unsigned char setBit(unsigned char reg, int pos) {
    return reg | (1 << pos);  // Set the bit without affecting other bits
    }

    int main() {
        unsigned char reg;
            int pos;

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

                    unsigned char updatedReg = setBit(reg, pos);
                        printf("%u\n", updatedReg);  // Print the updated register value

                            return 0;
                            }

Solving Approach
 

 

 

Upvote
Downvote
Loading...

Input

5 1

Expected Output

7