#include <stdio.h> unsigned char modifyBit(unsigned char reg, int pos, int mode) { if (mode) return reg | (1<<pos); return reg & ~(1<<pos); } int main() { unsigned char reg; int pos, mode; scanf("%hhu %d %d", ®, &pos, &mode); printf("%d", modifyBit(reg, pos, mode)); return 0; }
mode 1 : that is to set bit
doing OR operation is to set the particular bit by left shifting to that position.
mode 0: that is to clear bit
doing AND operation with 0 at that particular position,
inorder to do that we first shift 1 to that particular position then compliment all the bits.later do AND operation with register.
Test Cases
Test Results
Input
10 3 1
Expected Output
10