#include <stdio.h>
unsigned char modifyBit(unsigned char reg, int pos, int mode) {
if (mode == 1){
// reg = 00001010
// pos = 3
// mode = 1
// bit used to wise 1 = 0x01
// set bit mean that we OR the reg with the bit used to wise 0x01 (this bit type
//change according to type of the reg) << mean shift the bit 0x01 to the left
//pos index mean that 00000001 in to 00001000
// Then 00001010 | 00001000 = 00001010 follow OR rule
reg = reg | (0x01 << pos);
}
else {
// Clear bit mean that we use AND with the NOT of the shifted wise bit, when
//NOT the wise bit it turn into all 1 except the bit we want the reg to
//clear will be 0 then follow the AND rule 1 AND 0 will turn in to 0, all bit
//AND with 1 will remain the same
// 00001010 & ~(00001000) = 00001010 & 11110111 = 00000010
reg = reg & ~(0x01 << 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;
}
Input
10 3 1
Expected Output
10