Step 1: First you need to check the mode if the mode is 1 set the bit else clear the bit.
Step 2: set means r|(1<<p). Clear means r& ~(1<<p)
r|(1<<p)
r& ~(1<<p)
#include <stdio.h> unsigned char modifyBit(unsigned char r, int p, int m) { if (m==1){ return r|(1<<p); } else{ return r& ~(1<<p); } } int main() { unsigned char reg; int pos, mode; scanf("%hhu %d %d", ®, &pos, &mode); printf("%d", modifyBit(reg, pos, mode)); return 0; }
Test Cases
Test Results
Input
10 3 1
Expected Output
10