5. Clear the Bit in an 8-bit Register

Back To All Submissions
Previous Submission
Next Submission

Code

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

uint8_t clear_bit(uint8_t reg, uint8_t pos) {
    // Your code here
    uint8_t mask = 1 << pos;
    reg = reg & ~mask;
    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

本练习要求编写一个函数,在不影响其他位的情况下,将 8-bit 控制寄存器中指定位置(position)的数值清零。该操作通过构造一个特定位的掩码(mask),并结合按位取反(~)与按位与(&)逻辑运算,实现对硬件寄存器状态的精确修改。

 

 

Was this helpful?
Upvote
Downvote