Set Specific Bits in a 32-bit Register

Code

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

int set_bits (unsigned int reg, int pos, int len) {
    // Your code her
    int n=0;
    for(int i =0;i<len;i++){
        n |= (1<<i);
    }
    reg |= (n<<pos);
    return reg;
}

int main() {
    unsigned int reg;
    unsigned int pos, len;
    scanf("%u %u %u", &reg, &pos, &len);
    printf("%u", set_bits(reg, pos, len));
    return 0;
}

Solving Approach

 

 

 

Upvote
Downvote
Loading...

Input

0 4 3

Expected Output

112