#include <stdio.h>
#include <stdint.h>
uint8_t set_bit(uint8_t reg, uint8_t pos) {
// Your code here
return reg | (1<<pos);
}
int main() {
uint8_t reg, pos;
scanf("%hhu %hhu", ®, &pos); // Accept register value and position
uint8_t result = set_bit(reg, pos);
printf("%u", result); // Output the result as an integer
return 0;
}
Solving Approach
✅ Set Bit in 8-bit Register — Small & Compact Approach
1️⃣ Create mask for the target bit
1 << pos
This generates a value where only the pos bit is 1.
2️⃣ Use OR to set the bit
reg | (1 << pos)
Why OR?
0 | 1 = 1 → sets the bit
1 | 1 = 1 → remains 1
Other bits unchanged
💡 Final One-Line Logic
return reg | (1 << pos);
✔ Sets only the specified bit ✔ Does not affect other bits ✔ Constant time ✔ Embedded-friendly