#include <stdio.h> #include<math.h> unsigned char modifyBit(unsigned char reg, int pos, int mode) { // Write your code here int k,y[9],j,i=0,sum=0,x; k=(int)reg; while (k>0) { x=k%2; k=k/2; y[i]=x; i++; } for (j=i;j<8;j++) { y[j]=0; } if (y[pos]==mode) { return (int)reg; } else { y[pos]=mode; for (i=0;i<8;i++) { sum += y[i] * (1 << i); } return sum; } } 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