Clear the Bit in an 8-bit Register

NabilMohamed
NabilMohamed

Code

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

uint8_t clear_bit(uint8_t reg, uint8_t pos) {
    reg = reg & ~(1 << pos);
    return reg;
}

int main() {
    uint8_t reg, pos;
    scanf("%hhu %hhu", &reg, &pos);
    uint8_t result = clear_bit(reg, pos);
    printf("%u", result);
    return 0;
}

Solving Approach

_ _ _ ...  k ... _ (the given reg value; we focus on the kth bit)

## our filter

0 0 0 ... 1 ... 0 (1 left shifted by <pos> steps)

1 1 1 ... 0 ... 1 (take the complement of the above filter to get our final filter value)

If we logical AND the above filter with reg, we'll 0 out the <pos>th bit of reg.

 

 

 

Loading...

Input

7 0

Expected Output

6