Set or Clear a Specific Bit in a Register

Code

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

uint8_t set_clear(uint8_t reg, int pos, int mode)
{
    if (mode == 1)
        return reg |= (1 << pos);
    else
        return reg &= ~(1 << pos);
}

int main()
{
    uint8_t reg;
    int pos, mode;
    scanf("%hhu %d %d", &reg, &pos, &mode);
    printf("%d\n", set_clear(reg, pos, mode));
    return 0;
}

Solving Approach

 

 

 

Upvote
Downvote
Loading...

Input

10 3 1

Expected Output

10