25. Pack Multiple Fields into a 16-bit Control Register

Back To All Submissions
Previous Submission
Next Submission

Code

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

#define SET_MODE(val)       ( (val << 0) )
#define SET_SPEED(val)      ( (val << 3) )
#define SET_STATUS(val)     ( (val << 10) )
uint16_t pack_register(uint8_t mode, uint8_t speed, uint8_t status) {
    // Your logic here
    uint16_t ctrl = 0;

    ctrl |= SET_MODE(mode);
    ctrl |= SET_SPEED(speed);
    ctrl |= SET_STATUS(status);
    return ctrl;
}

int main() {
    uint8_t mode, speed, status;
    scanf("%hhu %hhu %hhu", &mode, &speed, &status);

    uint16_t reg = pack_register(mode, speed, status);
    printf("%u", reg);
    return 0;
}

Solving Approach

 

 

 

Was this helpful?
Upvote
Downvote