All submissions

Set or Clear a Specific Bit in a Register

Code

#include <stdio.h>

unsigned char modifyBit(unsigned char reg, int pos, int mode) {
    if (mode == 1){
        reg |= (1 << pos);
    } else{
        reg &= ~(1<< pos) ;
    }
    return reg;
    // Write your code here
}

int main() {
    unsigned char reg;
    int pos, mode;
    scanf("%hhu %d %d", &reg, &pos, &mode);
    printf("%d", modifyBit(reg, pos, mode));
    return 0;
}

Solving Approach

First Understand Problem

Count start from right with index 0

Let's suppose 

reg=00001010

Pos = 3

If mode =1

    Required results=00001010

    No change because 3 rd digit already 1.

If mode=0

     Required results=00000010

     change because 3 rd digit is 1.

   

 

 

Loading...

Input

10 3 1

Expected Output

10