Pack Multiple Fields into a 16-bit Control Register

Code

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

int main() {
    uint16_t mode, speed, status;
    uint16_t reg = 0;

    scanf("%hu %hu %hu", &mode, &speed, &status);

    reg = (mode & 0x7) |
          ((speed & 0x1F) << 3) |
          ((status & 0x3F) << 10);

    printf("%u", reg);
    return 0;
}

Solving Approach

 

 

 

Upvote
Downvote
Loading...

Input

3 10 12

Expected Output

12371