Set or Clear a Specific Bit in a Register

Code

#include "stdio.h"
#include <stdint.h>

uint8_t my_function(unsigned char reg,int pos, int mode) {
    reg=(reg & ~(1<<pos) )|(mode<<pos);
    return reg;
}

int main() {
    uint8_t reg=0;
    int pos=0;
    int mode=0;
    scanf("%d",&reg);
    scanf("%d",&pos);
    scanf("%d",&mode);
    printf("%d",my_function(reg,pos,mode));
    return 0;
}

Solving Approach

 

 

 

Upvote
Downvote
Loading...

Input

10 3 1

Expected Output

10