Extract a Bit Field from a 32-bit Register

Code

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

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

uint32_t extractField(uint32_t reg, int pos, int length) {
    // Create a mask with 'length' number of 1s
    uint32_t mask = (1u << length) - 1;

    // Shift down, then mask
    return (reg >> pos) & mask;
}

int main() {
    uint32_t reg;
    int pos, length;

    scanf("%u %d %d", &reg, &pos, &length);

    printf("%u", extractField(reg, pos, length));

    return 0;
}

Solving Approach

 

 

 

Upvote
Downvote
Loading...

Input

3060793344 28 4

Expected Output

11