20. Clear Specific Bits in a 32-bit Register

Back To All Submissions
Previous Submission
Next Submission

Code

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

uint32_t clear_bits(uint32_t reg, uint8_t pos, uint8_t len) {
    // Your code here
    uint32_t Value = 1;
    if ( len != 0 )
    {
        while ( len != 0 )
        {
            Value = Value * 2;
            len--;
        }
        Value--;
        Value = ( Value << pos ); 
        Value = (uint32_t)~Value;
        reg = reg & Value;
    }
    return reg;
}

int main() {
    uint32_t reg;
    uint8_t pos, len;
    scanf("%u %hhu %hhu", &reg, &pos, &len);
    printf("%u", clear_bits(reg, pos, len));
    return 0;
}

Solving Approach

 

 

 

Was this helpful?
Upvote
Downvote