#include <stdio.h>
unsigned char modifyBit(unsigned char reg, int pos, int mode) {
if(mode==1)
{
reg=reg|(1<<pos);
}
else
{
reg=reg&~(1<<pos);
}
return reg;
}
int main() {
unsigned char reg;
int pos, mode;
scanf("%hhu %d %d", ®, &pos, &mode);
printf("%d", modifyBit(reg, pos, mode));
return 0;
}
just passing the values of registers , bit position and operative mode as an arguments of function modifyBit
Then by using conditional statements (if and else) to choose the mode
IF condition:
if the value of mode is equal to one , then we need to set the specified bit that we passed in function by
i) Left shift the binary number 1 to Pos value
ii)doing bitwise OR between reg and shifted value
Else condition:
i) Left shift the binary number 1 to Pos value
ii)perform bitwise NOT to flip the bits
iii)Then perform bitwise AND between reg and flipped bits
Then return the value of reg
Input
10 3 1
Expected Output
10